JAVASCRIPT
NUMBER
In JavaScript, Numberis 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 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.
letnum = 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.
letnum = 255;
console.log(num.toString(16)); // Output: "ff"
valueOf(): Returns the primitive value of the specified object.
letnum = newNumber(123);
console.log(num.valueOf()); // Output: 123
JAVASCRIPT
BOOLEAN
In JavaScript, booleanis a primitive data type that represents a logical value. A
booleanvalue can be either true or false. The booleandata type is commonly
used in conditional statements, loops, and other control structures to make
decisions based on the truthiness or falsinessof 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 Stringobject 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.
StringMethods
charAt(): Returns the character at the specified index.
letstr = "Hello, World!";
console.log(str.charAt(0)); // Output: "H“
charCodeAt(): Returns the Unicode value of the character at the specified index.
letstr = "Hello";
console.log(str.charCodeAt(0)); // Output: 72 (ASCII value of "H")
concat():Combines two or more strings.
letstr1 = "Hello"; letstr2 = "World";
console.log(str1.concat(", ", str2));// Output: "Hello, World"
indexOf(): Returns the index of the first occurrence of a specified substring.
letstr = "Hello, World!";
console.log(str.indexOf("o")); // Output: 4
lastIndexOf(): Returns the index of the last occurrence of a specified substring.
letstr = "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.
letstr = "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.
letstr = "Visit W3Schools!";
console.log(str.search("W3Schools")); // Output: 6
slice(): Extracts a section of a string and returns a new string.
letstr = "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.
letstr = "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.
letstr = "Hello, World!";
console.log(str.substr(7, 5)); // Output: "World"