Javascript day 3

Numbers

we can use underscore (_) as a numeric separator to make it easier to read.

let nb = 1_000_000; // equivalent to 1000000
// 10_00 or 1_000 are equivalent to 1000

Converting from number to string

Though rarely used, we can do it by calling .toString() method.

Documentation on the web

When we see something similar to this String.prototype.toString(). For now, it means there is a method .something() that we can call on a String

NaN

It stands for Not a Number. It's often a sign of a bug.

Convert string to number

Number.parseInt(string, radix); Number is included because there's a global object called Number which contains a method called parseInt(). Make sure to always specify the radix to avoid bad surprises.

Use cases for converting a string to a number

THe most common case is when the number is entered by the user in a text box or the number is being read from the DOM

Number.parseInt("123abc", 10); // 123
Number.parseInt("5 meters", 10) // 5
Number.parseInt("teaaast 8", 10) // NaN
return Number.parseInt(age, 10) + 1; // return next year age

Operations

Use % to returns the division remainder. We'll use this to check whether a number is even or odd.

Number methods

There are some other methods we could call on numbers which are not commonly used. But we use the Math object which contains methods such as min(), max(), round(), etc.

Math.round(2.1); // 2
Math.round(2.6); // 3
Math.round(2.5); // 3

Math.floor(2.1); // 2
Math.floor(2.6); // 2
Math.floor(2.5); // 2

Math.ceil(2.1); // 3
Math.ceil(2.6); // 3
Math.ceil(2.5); // 3

This is it for today. Next up is Variables!

Numbers

We can use an underscore (_) as a numeric separator to make numbers easier to read.

let nb = 1_000_000; // equivalent to 1000000
// 10_00 or 1_000 are equivalent to 1000

Converting from number to string

Though rarely used, we can do this by calling the .toString() method.

Documentation on the web

When we see something like String.prototype.toString(), it means there is a method .something() that we can call on a String.

NaN

It stands for "Not a Number." It's often a sign of a bug.

Convert string to number

Number.parseInt(string, radix);. Number is included because there's a global object called Number that contains a method called parseInt(). Always specify the radix to avoid unexpected results.

Use cases for converting a string to a number

The most common case is when the number is entered by the user in a text box or when the number is being read from the DOM.

Number.parseInt("123abc", 10); // 123
Number.parseInt("5 meters", 10); // 5
Number.parseInt("teaaast 8", 10); // NaN
return Number.parseInt(age, 10) + 1; // returns next year's age

Operations

Use % to return the division remainder. We'll use this to check whether a number is even or odd.

Number methods

There are some other methods we could call on numbers, but they are not commonly used. Instead, we use the Math object, which contains methods such as min(), max(), round(), etc.

Math.round(2.1); // 2
Math.round(2.6); // 3
Math.round(2.5); // 3
Math.floor(2.1); // 2
Math.floor(2.6); // 2
Math.floor(2.5); // 2
Math.ceil(2.1); // 3
Math.ceil(2.6); // 3
Math.ceil(2.5); // 3

This is it for today. Next up is Variables!