- JavaScript Introduction
- JavaScript Where To
- JavaScript Output
- JavaScript Statements
- JavaScript Syntax
- JavaScript Comments
- JavaScript Variables
- JavaScript Let
- JavaScript Const
- JavaScript Operators
- JavaScript Assignment
- JavaScript Data Types
- JavaScript Functions
- JavaScript Objects
- JavaScript Events
- JavaScript Strings
- JavaScript String Methods
- JavaScript Numbers
- JavaScript Number Methods
- JavaScript Arrays
- JavaScript Array Const
- JavaScript Array Methods
- JavaScript Sorting Arrays
- JavaScript Array Iteration
- JavaScript Date Objects
- JavaScript Date Formats
- JavaScript Get Date Methods
- JavaScript Set Date Methods
- JavaScript Math Object
- JavaScript Random
- JavaScript Booleans
- JavaScript Comparison And Logical Operators
- JavaScript If Else And Else If
- JavaScript Switch Statement
- JavaScript For Loop
- JavaScript Break And Continue
- JavaScript Type Conversion
- JavaScript Bitwise Operations
- JavaScript Regular Expressions
- JavaScript Errors
- JavaScript Scope
- JavaScript Hoisting
- JavaScript Use Strict
- The JavaScript This Keyword
- JavaScript Arrow Function
- JavaScript Classes
- JavaScript JSON
- JavaScript Debugging
- JavaScript Style Guide
- JavaScript Common Mistakes
- JavaScript Performance
- JavaScript Reserved Words
- JavaScript Versions
- JavaScript History
- JavaScript Forms
- JavaScript Validation API
- JavaScript Objects
- JavaScript Object Properties
- JavaScript Function Definitions
- JavaScript Function Parameters
- JavaScript Function Invocation
- JavaScript Closures
- JavaScript Classes
- Java Script Async
- JavaScript HTML DOM
- The Browser Object Model
- JS Ajax
- JavaScript JSON
- JavaScript Web APIs
- JS Vs JQuery
JavaScript Numbers
JavaScript Numbers
JavaScript has only one type of number. Numbers can be written with or without decimals.
Example
var y = 3; // A number without decimals
Extra large or extra small numbers can be written with scientific (exponent) notation:
Example
var y = 123e-5; // 0.00123
JavaScript Numbers are Always 64-bit Floating Point
Unlike many other programming languages, JavaScript does not define different types of numbers, like integers, short, long, floating-point etc.
JavaScript numbers are always stored as double precision floating point numbers, following the international IEEE 754 standard.
This format stores numbers in 64 bits, where the number (the fraction) is stored in bits 0 to 51, the exponent in bits 52 to 62, and the sign in bit 63:
Value (aka Fraction/Mantissa) | Exponent | Sign |
---|---|---|
52 bits (0 - 51) | 11 bits (52 - 62) | 1 bit (63) |
Precision
Integers (numbers without a period or exponent notation) are accurate up to 15 digits.
Example
var y = 9999999999999999; // y will be 10000000000000000
The maximum number of decimals is 17, but floating point arithmetic is not always 100% accurate:
Example
To solve the problem above, it helps to multiply and divide:
Example
Practice Excercise Practice now
Adding Numbers And Strings
JavaScript uses the + operator for both addition and concatenation.
Numbers are added. Strings are concatenated.
If you add two numbers, the result will be a number:
Example
var y = 20;
var z = x + y; // z will be 30 (a number)
If you add two strings, the result will be a string concatenation:
Example
var y = "20";
var z = x + y; // z will be 1020 (a string)
If you add a number and a string, the result will be a string concatenation:
Example
var y = "20";
var z = x + y; // z will be 1020 (a string)
If you add a string and a number, the result will be a string concatenation:
Example
var y = 20;
var z = x + y; // z will be 1020 (a string)
A common mistake is to expect this result to be 30:
Example
var y = 20;
var z = "The result is: " + x + y;
A common mistake is to expect this result to be 102030:
Example
var y = 20;
var z = "30";
var result = x + y + z;
The JavaScript interpreter works from left to right.
First 10 + 20 is added because x and y are both numbers.
Then 30 + "30" is concatenated because z is a string.
Practice Excercise Practice now
Numeric Strings
JavaScript strings can have numeric content:
var y = "100"; // y is a string
JavaScript will try to convert strings to numbers in all numeric operations:
This will work:
var y = "10";
var z = x / y; // z will be 10
This will also work:
var y = "10";
var z = x * y; // z will be 1000
And this will work:
var y = "10";
var z = x - y; // z will be 90
But this will not work:
var y = "10";
var z = x + y; // z will not be 110 (It will be 10010)
Practice Excercise Practice now
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
However, if the string contains a numeric value , the result will be a number:
Example
You can use the global JavaScript function isNaN()
to find out if a value is a number:
Example
isNaN(x); // returns true because x is Not a Number
Watch out for NaN
. If you use NaN
in a mathematical operation, the result will also be NaN
:
Example
var y = 5;
var z = x + y; // z will be NaN
Or the result might be a concatenation:
Example
var y = "5";
var z = x + y; // z will be NaN5
NaN
is a number: typeof NaN
returns number
:
Example
Practice Excercise Practice now
Infinity
Infinity
(or -Infinity
) is the value JavaScript will return if you calculate a number outside the largest possible number.
Example
while (myNumber != Infinity) { // Execute until Infinity
myNumber = myNumber * myNumber;
}
Division by 0 (zero) also generates Infinity
:
Example
var y = -2 / 0; // y will be -Infinity
Infinity
is a number: typeof Infinity
returns number
.
Example
Practice Excercise Practice now
Hexadecimal
JavaScript interprets numeric constants as hexadecimal if they are preceded by 0x.
Example
Never write a number with a leading zero (like 07).
Some JavaScript versions interpret numbers as octal if they are written with a leading zero.
By default, JavaScript displays numbers as base 10 decimals.
But you can use the toString()
method to output numbers from base 2 to base 36.
Hexadecimal is base 16. Decimal is base 10. Octal is base 8. Binary is base 2.
Example
myNumber.toString(10); // returns 32
myNumber.toString(32); // returns 10
myNumber.toString(16); // returns 20
myNumber.toString(8); // returns 40
myNumber.toString(2); // returns 100000
Practice Excercise Practice now
Numbers Can Be Objects
Normally JavaScript numbers are primitive values created from literals:
var x = 123;
But numbers can also be defined as objects with the keyword new
:
var y = new Number(123);
Example
var y = new Number(123);
// typeof x returns number
// typeof y returns object
Do not create Number objects. It slows down execution speed.
The new
keyword complicates the code. This can produce some unexpected results:
When using the ==
operator, equal numbers are equal:
Example
var y = new Number(500);
// (x == y) is true because x and y have equal values
When using the ===
operator, equal numbers are not equal, because the ===
operator expects equality in both type and value.
Example
var y = new Number(500);
// (x === y) is false because x and y have different types
Or even worse. Objects cannot be compared:
Example
var y = new Number(500);
// (x == y) is false because objects cannot be compared
Note the difference between
(x==y)
and (x===y)
.Comparing two JavaScript objects will always return
false
. Practice Excercise Practice now
Products
Partner
Copyright © RVR Innovations LLP 2024 | All rights reserved - Mytat.co is the venture of RVR Innovations LLP