number(JavaScript Variable data type)

03/23/2023

Japanese version.

The number data type in JavaScript is used to represent numeric values, including both integers and decimals.

JavaScript's number data type uses 64-bit floating point numbers to represent values, allowing for very large or very small numbers, as well as numbers with many decimal places. However, floating point numbers have limited precision, so care must be taken when comparing or performing calculations with them.

JavaScript also includes special values to represent certain numeric values. Infinity represents positive infinity, while -Infinity represents negative infinity. NaN stands for Not a Number, and is used to represent invalid numeric values.

There are various operators available in JavaScript for performing arithmetic operations on numeric values, such as addition, subtraction, multiplication, division, and modulus. Additionally, there are functions available for converting values to numbers. parseInt() converts a string to an integer, while parseFloat() converts a string to a floating point number.

Here are some examples of declaring and performing operations with numeric values in JavaScript:

// Declaring numeric values
let a = 10; // integer declaration
let b = 3.14; // decimal declaration

// Performing operations with numeric values
let c = a + b; // addition
let d = a - b; // subtraction
let e = a * b; // multiplication
let f = a / b; // division
let g = a % 3; // modulus operator

console.log(c); // 13.14
console.log(d); // 6.86
console.log(e); // 31.4
console.log(f); // 3.1847133757961785
console.log(g); // 1

Here's an example of converting a string to a number using the parseInt() function:

let str = "42";
let num = parseInt(str); // convert string to integer
console.log(num); // 42

And here's an example of converting a string to a floating point number using the parseFloat() function:

let str = "3.14";
let num = parseFloat(str); // convert string to floating point number
console.log(num); // 3.14