Skip to main content
JAVASCRIPT
NUMBER
In JavaScript, Number is a built-in object that represents numerical
values and provides methods and properties for working with numbers.
It is one of the primitive data types and can represent both integer and
floating-point numbers.
Number Properties
Properties are values associated with an object that provide information or
settings related to the object. They are accessed using the dot notation.
Some properties of Number are as follows:
Number.MAX_VALUE: Returns the largest possible number in JavaScript.
Number.MIN_VALUE: Returns the smallest possible positive number close to
zero.
Number.NaN: Represents "Not-a-Number", which indicates an
unrepresentable value resulting from an undefined or erroneous operation.
Number.NEGATIVE_INFINITY: Represents negative infinity.
Number.POSITIVE_INFINITY: Represents positive infinity.
Number Methods
Methods are functions associated with an object that perform actions or
operations related to the object. They are invoked using parentheses.
Some Methods of Number are as follows:
(let num = 123.456;)
toExponential(): Converts a number into exponential notation.
console.log(num.toExponential(2)); // Output: "1.23e+3"
toFixed(): Formats a number using fixed-point notation.
console.log(num.toFixed(2)); // Output: "123.46"
toLocaleString(): Returns a string representation of the number formatted
according to the locale-specific conventions.
let num = 123456.789;
console.log(num.toLocaleString('en-US')); // Output: "123,456.789"
toPrecision(): Returns a string representation of the number with a specified
precision.
console.log(num.toPrecision(4)); // Output: "123.5"
toString(): Returns a string representation of the number. Radix can also be
given as parameter for different bases.
let num = 255;
console.log(num.toString(16)); // Output: "ff"
valueOf(): Returns the primitive value of the specified object.
let num = new Number(123);
console.log(num.valueOf()); // Output: 123
JAVASCRIPT
BOOLEAN
In JavaScript, boolean is a primitive data type that represents a logical value. A
boolean value can be either true or false. The boolean data type is commonly
used in conditional statements, loops, and other control structures to make
decisions based on the truthiness or falsiness of a condition.
Boolean Methods
toSource(): Returns a string representing the source code of the Boolean
object.
toString(): Returns a string representation of the Boolean object.
valueOf(): Returns the primitive value of the Boolean object.
JAVASCRIPT
STRING
In JavaScript, the String object is a global object and a wrapper around
a sequence of characters, which allows you to work with strings as
objects. The String object has properties that provide information about
the string.
String Properties
Length: Returns the length of the string.
String Methods
charAt(): Returns the character at the specified index.
let str = "Hello, World!";
console.log(str.charAt(0)); // Output: "H“
charCodeAt(): Returns the Unicode value of the character at the specified index.
let str = "Hello";
console.log(str.charCodeAt(0)); // Output: 72 (ASCII value of "H")
concat(): Combines two or more strings.
let str1 = "Hello"; let str2 = "World";
console.log(str1.concat(", ", str2)); // Output: "Hello, World"
indexOf(): Returns the index of the first occurrence of a specified substring.
let str = "Hello, World!";
console.log(str.indexOf("o")); // Output: 4
lastIndexOf(): Returns the index of the last occurrence of a specified substring.
let str = "Hello, World!";
console.log(str.lastIndexOf("o")); // Output: 8
replace(): Searches a string for a specified value or regular expression and
replaces it with a new value.
let str = "Hello, World!";
console.log(str.replace("World", "Universe")); // Output: "Hello, Universe!"
search(): Searches a string for a specified value or regular expression and
returns the position of the first match.
let str = "Visit W3Schools!";
console.log(str.search("W3Schools")); // Output: 6
slice(): Extracts a section of a string and returns a new string.
let str = "Apple, Banana, Cherry";
console.log(str.slice(7, 13)); // Output: "Banana"
split(): Splits a string into an array of substrings based on a specified separator.
let str = "Apple, Banana, Cherry";
console.log(str.split(", ")); // Output: ["Apple", "Banana", "Cherry"]
substr(): Extracts a portion of a string, starting from a specified index, and returns a
new string.
let str = "Hello, World!";
console.log(str.substr(7, 5)); // Output: "World"