Numbers in JavaScript represent numeric values and can be used for various mathematical operations and calculations. Here are some key points about numbers in JavaScript:
-
Number Data Type: JavaScript has a single numeric data type for all numeric values, including both integers and floating-point numbers.
-
Integer and Floating-Point: Numbers can be integers (whole numbers) or floating-point (decimal numbers). JavaScript represents all numbers as 64-bit floating-point values.
-
Literals: You can represent numbers in JavaScript using numeric literals, such as
1,3.14,0.25, etc. -
Arithmetic Operations: You can perform standard arithmetic operations on numbers, including addition, subtraction, multiplication, and division.
const a = 5;
const b = 3;
const sum = a + b; // Sum is 8- Math Object: JavaScript provides a built-in
Mathobject that contains various mathematical functions and constants, such asMath.sqrt(),Math.pow(), andMath.PI.
const squareRoot = Math.sqrt(16); // squareRoot is 4- NaN (Not-a-Number):
NaNis a special value that represents an undefined or unrepresentable value. It is often returned when a mathematical operation is not valid.
const result = 0 / 0; // result is NaN- Infinity: JavaScript represents positive and negative infinity using the values
Infinityand-Infinity.
const positiveInfinity = Infinity;
const negativeInfinity = -Infinity;- Parsing and Conversion: You can parse strings to numbers using functions like
parseInt()andparseFloat(), and convert numbers to strings usingtoString().
const str = "42";
const num = parseInt(str); // num is 42
const str2 = num.toString(); // str2 is "42"- Number Methods: The Number object provides various methods and properties for working with numbers, such as
Number.isInteger(),Number.isNaN(), andNumber.MAX_VALUE.
const isInteger = Number.isInteger(42); // isInteger is true- Rounding and Precision: JavaScript has methods like
toFixed(),toPrecision(), andtoExponential()for formatting and rounding numbers.
const pi = Math.PI; // pi is approximately 3.141592653589793
const piFixed = pi.toFixed(2); // piFixed is "3.14"Numbers are fundamental in JavaScript and play a crucial role in many programming tasks, ranging from simple calculations to more complex mathematical operations.
const a = 3;
const b = 3.13;
console.log(typeof(a));
console.log(typeof(b));Output
number
number
const str = '23';
console.log(typeof(str));
const num = Number(str);
console.log(typeof(num));Output
string
number
const bool = true;
const result = Number(bool);
console.log(result);Output
1
const str = 'hello';
const result = Number(str);
console.log(result);Output
NaN
const result = 4 - 'hello';
console.log(result);Output
NaN
const result = isNaN((9);
console.log(result);Output
false
const result = isNaN('hello');
console.log(result);Output
true
const num = 32;
const result = Number.isInteger(num);
console.log(result);Output
true
const num = 32.56;
const result = Number.isInteger(num);
console.log(result);Output
false
const num = '5';
const result = Number.parseInt(num);
console.log(result);
console.log(Number.isInteger(result));Output
5
true
const num = 5.234234324324;
const result = Number.parseInt(num);
console.log(result);
console.log(Number.isInteger(result));Output
5
true
const num = '5.23423423';
const result = Number.parseFloat(num);
console.log(result);
console.log(typeof(result));Output
5.23423423
number
const num = 5.234234324324;
const result = num.toFixed(2);
console.log(result);Output
5.23
Take numeric string input from the user. Convert that input into numbers. If the input has been converted to numbers, multiply that number by 10 and display the output. Else it is not converted to numbers, display "The input is not a numeric string."
const numberInput = prompt("Enter a number: ");
const result = Number(numberInput);
if(typeof(result) == "number") {
console.log(result * 10);
} else {
console.log("The input is not a numeric string.");
}Output
Enter a number: 5.5
55
Q. What is the output of the program?
const num = '23' - '55';
console.log(num);- Error
- NaN
- 32
- -32
Answer: 4