Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

Hexadecimal

Numeric constants in JavaScript are interpreted as hexadecimal if they are prefixed by 0x.

Example

let x = 0xFF;
Avoid writing numbers with a leading zero, such as 07. Certain versions of JavaScript may interpret numbers as octal if they are formatted in this manner.

By default, JavaScript presents numbers as base 10 decimals.

However, you can utilize the toString() method to output numbers ranging from base 2 to base 36.

Hexadecimal is base 16, decimal is base 10, octal is base 8, and binary is base 2.

Example

let myNumber = 32;
myNumber.toString(32);
myNumber.toString(16);
myNumber.toString(12);
myNumber.toString(10);
myNumber.toString(8);
myNumber.toString(2);

JavaScript Numbers as Objects

Typically, JavaScript numbers are primitive values generated from literals.

let x = 123;

Numbers in JavaScript are usually primitive values created from literals, but they can also be defined as objects using the “new” keyword.

let y = new Number(123);

Example

let x = 123;
let y = new Number(123);
Avoid creating Number objects as they can complicate your code and slow down execution speed. Additionally, Number objects may produce unexpected results.

When employing the == operator, x and y are considered equal.

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

When utilizing the === operator, x and y are not considered equal.

let x = 500;
let y = new Number(500);
Take note of the distinction between (x==y) and (x===y).

(x == y) true or false?

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

(x === y) true or false?

let x = new Number(500);
let y = new Number(500);
When comparing two JavaScript objects, the result will always be false.