Skip to main content

Command Palette

Search for a command to run...

05. BASICS | Number and Math in JavaScript

This article will talk about number and math in javascript.

Updated
1 min readView as Markdown
S
Full-Stack Developer with 4 years' experience, specializing in backend development. Skilled in JavaScript, React, Python, Databases, and AWS. Known for building scalable web apps, leading teams, and maintaining strong client communication. Upskilling in Generative AI.

Number

const score = 400;
console.log(typeof(score)); // number

const balance = new Number(100);
console.log(typeof(balance)); // object


// Number properties and methods
// 1) toString()
console.log(balance.toString().length); // 3

// 2) toFixed() ==> how many digits will come after decimal
console.log(balance.toFixed(1)); // 100.0

// 3) toPrecision() ==> total kitni digits rahengi after round-off
const otherNumber = 123.8966
console.log(otherNumber.toPrecision(4)); // 123.9

// 4) toLocaleString()
const hundreds = 1000000
console.log(hundreds.toLocaleString('en-IN')); // 10,00,000

// 5) Number.MAX_VALUE

// 6) Number.MIN_VALUE

// 7) Number.MAX_SAFE_INTEGER

// 8) Number.MIN_SAFE_INTEGER

Math

Math is an object. Math object has many properties and methods.

// Math methods
console.log(Math.abs(-4)); // 4
console.log(Math.round(4.6)); // 5
console.log(Math.ceil(4.2)); // 5
console.log(Math.floor(4.9)); // 4
console.log(Math.min(4, 3, 6, 8)); // 3
console.log(Math.max(4, 3, 6, 8)); // 8

console.log(Math.random()); // gives a random number between 0 and 1

console.log(8/0); // Infinity
console.log(-8/0); // -Infinity

console.log(0/0); // NaN
💡
typeof(NaN) = number
💡
typeof(Infinity) = number; typeof(-Infinity) = number

Happy reading!