JS Basic Datatypes - Number
Download (.odt) Download (Markdown)let integer = 21;
let floatingPointNumber = 3.14;
Basic operations
[hook]let addition = 6 + 3; // 9
let subtraction = 6 - 3; // 3
let multiplication = 6 * 3; // 18
let division = 6 / 3; // 2
let modulus = 6 % 3; // 0 (6 divided by 3 gives 0 as remainder)
Exponentiation
[hook]let exponentiation = Math.pow(3, 2); // 3 power 2 (3 is the power base, 2 is the power exponent. 3 power 2 is 9.)
Square root
[hook]let squareRoot = Math.sqrt(9); // √9 - square root of 9 is 3 (because 3 power 2 is 9)
Cube root
let cubeRoot = Math.cbrt(9); // ∛27 - cube root of 27 is 3 (because 3 power 3 is 27)
Round to nearest integer
[hook]let rounded = Math.round(3.14); // 3.14 is rounded to 3
let rounded2 = Math.round(3.5); // 3.5 is rounded to 4
let rounded3 = Math.round(3.69); // 3.69 is rounded to 4
Round down
let roundedDown = Math.floor(3.14); // 3.14 is rounded to 3
let roundedDown2 = Math.floor(3.5); // 3.5 is rounded to 3
let roundedDown3 = Math.floor(3.69); // 3.69 is rounded to 3
Round up
let roundedDown = Math.ceil(3.14); // 3.14 is rounded to 4
let roundedDown2 = Math.ceil(3.5); // 3.5 is rounded to 4
let roundedDown3 = Math.ceil(3.69); // 3.69 is rounded to 4
Absolute value
[hook]let absoluteValue = Math.abs(21); // 21
let absoluteValue2 = Math.abs(0); // 0
let absoluteValue3 = Math.abs(-21); // 21
Get biggest value
[hook]let biggest = Math.max(-21, 21, 3.14, -3.14); // 21
let array = [-21, 21, 3.14, -3.14];
let biggestItemOfArray = Math.max(...array); // 21
Get smallest value
[hook]let smallest = Math.min(-21, 21, 3.14, -3.14); // -21
let array = [-21, 21, 3.14, -3.14];
let smallestItemOfArray = Math.min(...array); // -21
Number PI
[hook]const PI = Math.PI; // 3.141592653589793
Number E
[hook]const E = Math.E; // 2.718281828459045
Generate a random number
[hook]Generate a random number between 0 (inclusive) and 1 (exclusive):
let random = Math.random();
Generate a random number between a min (inclusive) and a max (exclusive) number.
let min = 21;
let max = 69;
let random2 = Math.random() * (max - min) + min;
Generate a random integer between a min (inclusive) and a max (exclusive) number.
let min = 21;
let max = 69;
let random3 = Math.floor(Math.random() * (max - min) + min);
Generate a random integer between a min (inclusive) and a max (inclusive) number.
let min = 21;
let max = 69;
let random4 = Math.floor(Math.random() * (max - min + 1) + min);