Menu Close

JavaScript Numbers ( With Examples )

In this tutorial, you will learn about JavaScript Numbers. In the previous JavaScript tutorials, we have seen all about JavaScript type conversion with the help of the example. In JavaScript, a Number is a primitive data type.

What is the JavaScript Numbers

JavaScript numbers has only one type of number, number can be written with or without decimal.

Example:


var x = 12; // Number without decimal
var y = 12.45; // Number with decimal

You can also use exponential notation e to include too large or small number, for example:

Example:


const num1 = 5e;
console.log(num1);

const num2 = 5e - 10;
console.log(num2);

JavaScript Numbers are stored in 64-bit

Regular JavaScript numbers are stored in 64-bit format IEEE-754, also known as “double-precision floating pointing numbers“. These are the number that we are using most of the time in our JavaScript code.

Adding number and string

If you add two number, Result will be a number.

Example:


var x = 12;
var y = 34;
var result = x + y;
console.log(result); // result will be 46

If you add two string the result will be string.

Example:


let a = 'Programming ';
let b = 'Funda';
console.log(a + b); // Programming Funda

If you add a number and string the result will be string concatenation.

Example:


let x = 10;
let y = 30;
let z = '30';
var result = x + y + z;
console.log('The result is:- ', result); // result will be 4030

Note: JavaScript + operator is used to add numbers and concatenation. Numbers are added and strings are concatenated.

Number String

JavaScript string can have numeric string.

Example:


var x = '1020';
console.log(x); // x is a string

let a = 1000;
console.log(a); // a is number

JavaScript will try to convert strings to JavaScript numbers in all numeric operations.

Example:


let x = '1020';
let y = 10;
console.log( x / y); // output is 102

NaN – Not a Number

NaN is a JavaScript reserved word indicating that a number is not a legal number. Trying to do arithmetic with a non-numeric string will result in NaN (Not a Number).

Example:


var x = '100';
var y = 'hello';
var result = x / y;
console.log(result); // error

However, if the string contain the numeric value result will be number.

Example:


var x = '100';
var y = '10';
var result = x / y;
console.log(result); // result will be 10

If you want to check a number is NaN or not, then you can use global JavaScript function isNaN().

Example:


var x = '100';
var y = 'hello';
var result = x / y;
console.log(isNaN(result)); // return true because result is not a number.

If you use NaN in mathematical operations, Then result will be NaN.

Example:


let x = 12;
let y = NaN;
console.log(x + y); // output will be NaN

or result might be concatenation.


let x = '12';
let y = NaN;
console.log(x + y); // output will be 12NaN

Hexadecimal

JavaScript interprets numeric constants as hexadecimal if they are preceded by 0x.

Example:


var x = 0xFF;        // x will be 255
console.log('The result will be:- ', x);

JavaScript Numbers Can be Objects

Normally Number in JavaScript define as a literal. But number can also define as object with new keyword.

Example:


var x = 12;
var y = new Number(23);
console.log(x);
console.log(y);

When you using == operator, equal number are equal.

Example:


let x = 100;
let y = new Number(100);
console.log(x == y); // result will be true because both have same values.

When you use === operator, equal numbers are not equal because === operator checks equality of both value and type.

Example:


let x = 100;
let y = new Number(100);

console.log(typeof x); // number
console.log(typeof y); // object
console.log(x == y); // result will be true because both have same values.

Even object can not be compared.

Example:


let x = new Number(100);
let y = new Number(100);
console.log(x == y); // output will be false because object can not compared.

JavaScript Infinity

When the value of number calculation exceed out of possible value, then Infinity or -Infinity will return.

Example:


let x = 10 / 0;
console.log(a); // Infinity

let x = -10 / 0;
console.log(a); // - Infinity

Summary

So, here we have learned all about JavaScript Numbers with the help of examples.JavaScript number is a primitive data type, which means you can’t alter the number once it has declared.

I hope this article will have helped you. If you like this article please share and visit for Interesting and knowledgeable JavaScript tutorials.

Previous Article
Next Article

Was this post helpful?

Related Posts