JavaScript Tutorial

JS - Introduction
JS - Basic
JS - Placement
JS - Output
JS - Statements
JS - Syntax
JS - Variables
JS - Comments
JS - Data Types
JS - Operators
JS - Operator Precedence
JS - Condition
JS - Switch
JS - Functions
JS - Objects
JS - Loops For
JS - Loops While
JS - Break
JS - Strings
JS - String Methods
JS Objects

Object Definitions
Object Properties
Object Methods
Object Constructors
Object Prototypes
JS Functions

Function Definitions
Function Parameters
Function Call
Function Apply
Function Closures
JS HTML DOM

DOM Introduction
DOM Methods
DOM Selectors
DOM HTML
DOM CSS
DOM Attributes
DOM Navigation
JS Browser BOM

JS - Window
JS - Screen
JS - Location
JS - History
JS - Navigator
JS - Popup Alert
JS - Timing
JS - Cookies
JS AJAX

AJAX - Introduction
AJAX - XMLHttp
AJAX - Request
AJAX - Response
AJAX - PHP
JS JSON

JSON - Introduction
JSON - Syntax
JSON - Data Types
JSON - Parse
JSON - Stringify
JSON - Objects
JSON - Arrays
JSON - PHP
JSON - JSONP
JS References

JavaScript Reference
JavaScript Methods
HTML DOM Reference
JavaScript has only one number type, there is no separate designation for integers and floating-point numbers.

Because of this, numbers can be written in JavaScript with or without decimals.

`var a = 25; // without decimals`

`var b = 80.05; // with decimals`

In both cases above, the data type is a number and is the same regardless of whether or not the number has decimal points.

Scientific exponential notation can be used in JavaScript to abbreviate very large or small numbers, as in the following example:

`let num1 = 957e8; // 95700000000`

`let num2 = 957e-8; // 0.00000957`

Numbers in JavaScript are considered to be accurate up to 15 digits. That means that numbers will be rounded after the 16th digit is reached:

`let num1 = 999999999999999; // remains as 999999999999999`

`let num2 = 9999999999999999; // rounded up to 10000000000000000`

Unlike other languages, JavaScript does not define different types of numbers, like integers, short, long, floating-point etc.

JavaScript numbers have 64-bit precision, which is also called double precision.

The internal representation is based on the IEEE 754 standard.

The 64 bits are distributed between a number’s sign, exponent, and fraction as follows:

Sign | Exponent | Fraction |
---|---|---|

1 bit | 11 bits | 52 bits |

Bit 63 | Bits 62–52 | Bits 51–0 |

As you know from the previous chapters, the `+`

operator is used for both addition and concatenation.

Numbers are added. Strings are concatenated.

If you add two numbers, the result will be a number:

`let x = 50;`

`let y = 10;`

`let z = x + y; // z will be 60 (a number)`

If you add two strings, the result will be a string concatenation:

`let x = "50";`

`let y = "10";`

`let z = x + y; // z will be 5010 (a string)`

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

`let x = 50;`

`let y = "10";`

`let z = x + y; // z will be 5010 (a string)`

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

`let x = "50";`

`let y = 10;`

`let z = x + y; // z will be 5010 (a string)`

JavaScript evaluates expressions from left to right. Different sequences can produce different results.

In the below example, first 50 + 10 is added because x and y are both numbers, then 60 + "30" is concatenated because z is a string:

`let x = 50;`

`let y = 10;`

`let z = "30";`

`let sum = x + y + z; // sum will be 6030 (a string)`

A common mistake is to expect this result to be 60:

`let x = 50;`

`let y = 10;`

`let z = "The sum is: " + x + y;`

You can solve above problem by using parentheses:

`let x = 50;`

`let y = 10;`

`let z = "The sum is: " + (x + y);`

JavaScript strings can contain numeric values.

`let x = 50; // x is a number`

`let y = "50"; // y is a string`

If a string contains a numeric value, the mathematical expression can be performed in JavaScript.

Division can be performed:

`let x = "50" / "10"; // x will be 5`

Multiplication can be performed:

`let x = "50" * "10"; // x will be 500`

Exponentiation can be performed:

`let x = "5" ** "3"; // x will be 125`

Incrementation can be performed:

`let x = "50";`

`x++; // x will be 51`

Subtraction can be performed:

`let x = "50" - "10"; // x will be 40`

Addition cannot be performed:

`let x = "50" + "10"; // x will be 5010`

**Note:** If you add two strings, the result will be a string concatenation.

In JavaScript `NaN`

is a reserved word indicating that a number is not a legal number.

If you attempt to perform a mathematical operation on a number and a non-numeric value, `NaN`

will be returned.

`var x = 50 / "Parrot"; // x will be NaN (Not a Number)`

However, if a string contains a numeric value, the mathematical expression can be performed in JavaScript:

`var x = 50 / "10"; // x will be 5`

You can use the global JavaScript `isNaN()`

function to find out if a value is a number:

`var x = 50 / "Parrot";`

`isNaN(x); // returns true because x is Not a Number`

When assigning the value `NaN`

to a variable used in an operation, it will result in the value of `NaN`

, even when the other operand is a legal number:

`var x = NaN;`

`var y = 27;`

`var z = x + y; // z will be NaN`

`NaN`

is a number; `typeof NaN`

returns `number`

:

`typeof NaN; // will return "number"`

`Infinity`

or `-Infinity`

will be returned if you calculate a number outside of the largest possible number available in JavaScript.

These will also occur for values that are undefined, as when dividing by zero:

`var x = 5 / 0; // will return Infinity`

`var y = -5 / 0; // will return -Infinity`

In technical terms, `Infinity`

will be displayed when a number exceeds the number `1.797693134862315E+308`

, which represents the upper limit in JavaScript.

Similarly, `-Infinity`

will be displayed when a number goes beyond the lower limit of `-1.797693134862316E+308`

.

`Infinity`

is a number: `typeof Infinity`

returns `number`

:

`typeof Infinity; // will return "number"`

The number `Infinity`

can also be used in loops:

`var num = 5;`

`while (num != Infinity) {`

`// Code here will execute through num = Infinity`

`}`

By default, JavaScript displays numbers as **base 10** decimals.

Numbers can also be represented in hexadecimal (base 16), binary (base 2), and octal (base 8) notation.

Hexadecimal numbers are prefixed with `0x`

:

`var x = 0xFF; // x will be 255`

Binary numbers are prefixed with `0b`

:

`var x = 0b1111; // x will be 15`

Never write a number with a leading zero (like 0121). JavaScript interpret numbers as octal if they are written with a leading zero:

`var x = 016;`

You can use the `toString()`

method to return numbers from base 2 to base 36.

Hexadecimal is **base 16**, Decimal is **base 10**, Octal is **base 8**, Binary is **base 2**.

`var num = 255;`

`num.toString(10); // returns 255`

`num.toString(16); // returns ff`

`num.toString(8); // returns 377`

`num.toString(2); // returns 11111111`

Normally, JavaScript numbers are primitive values, created from literals:

var num = 50;

But numbers can also be defined as objects using the ` new`

keyword:

var num = new Number(50);

In order to test the difference between the two, we will initialize a number primitive and a number object.

`var num1 = 50;`

`var num2 = new Number(50);`

``

`typeof num1 // returns number`

`typeof num2 // returns object`

**Note:** Don't create numbers as objects. It slows down execution speed and can produce some unexpected results.

When using the `==`

operator, equal numbers are equal:

`var num1 = 50;`

`var num2 = new Number(50);`

``

`document.write(num1 == num2); // returns true because num1 and num2 have equal values`

When using the `===`

operator, equal numbers are not equal, because the `===`

operator expects equality in both value and type:

`var num1 = 50;`

`var num2 = new Number(50);`

``

`document.write(num1 === num2); // returns false because num1 and num2 have different types`

Objects cannot be compared:

`var num1 = new Number(50);`

`var num2 = new Number(50);`

``

`document.write(num1 == num2); // returns false because num1 and num2 are different objects`

`document.write(num1 === num2); // returns false because num1 and num2 are different objects`

Notice the difference between (`==`

) and (`===`

). Comparing two JavaScript objects will always return false.