# JavaScript tutorials for Beginners – JavaScript Number # (JavaScript Tutorials for Beginners)

In this end-to-end example, you will learn – JavaScript tutorials for Beginners – JavaScript Number.

# JavaScript Number

#### In this tutorial, you will learn about JavaScript Number with the help of examples.

In JavaScript, numbers are the primitive data types. For example,

``````let a = 3;
let b = 3.13;``````

Unlike in some other programming languages, you don’t have to specifically declare for integer or floating values using intfloat, etc.

You can use exponential notation e to include too large or too small numbers. For example,

``````let a1 = 5e9;
console.log(a1); //5000000000

let a2 = 5e-5;
console.log(a2); // 0.00005``````

Numbers can also be denoted in hexadecimal notation. For example,

``````let a = 0xff;
console.log(a); // 255

let b = 0x00 ;
console.log(b); // 0``````

## + Operator with Numbers

When `+` is used with numbers, it is used to add the numbers. For example,

``````let a = 4 + 9;
console.log(a); // 13``````

When `+` is used with numbers and strings, it is used to concatenate them. For example,

``````let a = '4' + 9;
console.log(a); // 49``````

When a numeric string is used with other numeric operations, the numeric string is converted to a number. For example,

``````let a = '4' - 2;
console.log(a); // 2

let a = '4' / 2;
console.log(a); // 2

let a = '4' * 2;
console.log(a); // 8``````

## JavaScript NaN

In JavaScript, `NaN`(Not a Number) is a keyword that indicates that the value is not a number.

Performing arithmetic operations( except `+` ) to numeric value with string results in `NaN`. For example,

``````let a = 4 - 'hello';
console.log(a); // NaN``````

The built-in function `isNaN()` can be used to find if a value is a number. For example,

``````let a = isNaN(9);
console.log(a); // false

let a = isNaN(4 - 'hello');
console.log(a); // true``````

When the `typeof` Operator is used for `NaN` value, it gives a number output. For example,

``````let a = 4 - 'hello';
console.log(a); // NaN
console.log(typeof a); // "number"``````

## JavaScript Infinity

In JavaScript, when the calculation is done that exceeds the largest possible number, `Infinity`( or `-Infinity`) is returned. For example,

``````let a = 2 / 0;
console.log(a); // Infinity

let a = -2 / 0;
console.log(a); // -Infinity``````

## JavaScript BigInt

In JavaScript, Number type can only represent numbers less than (253 – 1) and more than -(253 – 1). However, if you need to use a larger number than that, you can use the BigInt data type.

A BigInt number is created by appending n to the end of an integer. For example,

``````// BigInt value
let value = 900719925124740998n;

let value1 = value + 1n;
console.log(value1); // returns "900719925124740999n"``````

Note: BigInt was introduced in the newer version of JavaScript and is not supported by many browsers.

## JavaScript Numbers Are Stored in 64-bit

In JavaScript, numbers are stored in 64-bit format IEEE-754, also known as “double precision floating point numbers”.

The numbers are stored in 64 bits(the number is stored in 0 to 51 bits, the exponent in 52 to 62 bits and the sign in 63 bits)

Numbers Exponent Sign
52 bits(0 – 51) 11 bits(52- 62) 1 bit(63)

## Precision Problems

Operations on floating-point numbers results in some unexpected results. For example,

``````let a = 0.1 + 0.2;
console.log(a); // 0.30000000000000004``````

The result should be 0.3 instead of 0.30000000000000004. This error occurs because in JavaScript, numbers are stored in binary form to represent decimal digits internally. And decimal numbers can’t be represented in binary form exactly.

To solve the above problem, you can do something like this:

``````let a = (0.1 * 10 + 0.2 * 10) / 10;
console.log(a); // 0.3``````

You can also use the `toFixed()` method.

``````let a = 0.1 + 0.2;
console.log(a.toFixed(2)); // 0.30``````

`toFixed(2)` rounds up the decimal number to two decimal values.

``````let a = 9999999999999999
console.log(a); // 10000000000000000``````

Note: Integers are accurate up to 15 digits.

## Number Objects

You can also create numbers using the `new` keyword. For example,

``````let a = 45;

// creating a number object
let b = new Number(45);

console.log(a); // 45
console.log(b); // 45

console.log(typeof a); // "number"
console.log(typeof b); // "object"``````

Note: It is recommended to avoid using number objects. Using number objects slows down the program.

## JavaScript Number Methods

Here is the list of built-in number methods in JavaScript.

Method Description
isNaN() determines whether the passed value is NaN
isFinite() determines whether the passed value is a finite number
isInteger() determines whether the passed value is an integer
isSafeInteger() determines whether the passed value is a safe integer
parseFloat(string) converts the numeric floating string to floating-point number
parseInt(string, [radix]) converts the numeric string to integer
toExponential(fractionDigits) returns a string value for a number in exponential notation
toFixed(digits) returns a string value for a number in fixed-point notation
toPrecision() returns a string value for a number to a specified precision
valueof() returns the numbers value
toLocaleString() returns a string with a language sensitive representation of a number

For example,

``````// check if a is integer
let a = 12;
console.log(Number.isInteger(a)); // true

// check if b is NaN
let b = NaN;
console.log(Number.isNaN(b)); // true

// display upto two decimal point
let d = 5.1234;
console.log(d.toFixed(2)); // 5.12``````

## JavaScript Number Properties

Here is the list of Number properties in JavaScript.

Property Description
EPSILON returns the smallest interval between two representable numbers
MAX_SAFE_INTEGER returns the maximum safe integer
MAX_VALUE returns the largest possible value
MIN_SAFE_INTEGER returns the minimum safe integer
MIN_VALUE returns the smallest possible value
NaN represents ‘Not-a-Number’ value
NEGATIVE_INFINITY represents negative infinity
POSITIVE_INFINITY represents positive infinity
prototype allows the addition of properties to Number objects

For example,

``````// largest possible value
let a = Number.MAX_VALUE;
console.log(a); // 1.7976931348623157e+308

// maximum safe integer
let a = Number.MAX_SAFE_INTEGER;
console.log(a); // 9007199254740991``````

## JavaScript Number() Function

The `Number()` function is used to convert various data types to numbers. For example,

``````let a = '23'; // string
let b = true; // boolean

//converting to number
let result1 = Number(a);
let result2 = Number(b);

console.log(result1); // 23
console.log(result2); // 1``````

# Personal Career & Learning Guide for Data Analyst, Data Engineer and Data Scientist

## Applied Machine Learning & Data Science Projects and Coding Recipes for Beginners

A list of FREE programming examples together with eTutorials & eBooks @ SETScholars

# Projects and Coding Recipes, eTutorials and eBooks: The best All-in-One resources for Data Analyst, Data Scientist, Machine Learning Engineer and Software Developer

Topics included: Classification, Clustering, Regression, Forecasting, Algorithms, Data Structures, Data Analytics & Data Science, Deep Learning, Machine Learning, Programming Languages and Software Tools & Packages.
(Discount is valid for limited time only) `Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.`

# Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners

Please do not waste your valuable time by watching videos, rather use end-to-end (Python and R) recipes from Professional Data Scientists to practice coding, and land the most demandable jobs in the fields of Predictive analytics & AI (Machine Learning and Data Science).

The objective is to guide the developers & analysts to “Learn how to Code” for Applied AI using end-to-end coding solutions, and unlock the world of opportunities!