KEMBAR78
Javascript objects, properties and methods .pdf
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"

Javascript objects, properties and methods .pdf

  • 1.
    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.
  • 2.
    Number Properties Properties arevalues associated with an object that provide information or settings related to the object. They are accessed using the dot notation.
  • 3.
    Some properties ofNumber 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.
  • 4.
    Number.NEGATIVE_INFINITY: Represents negativeinfinity. Number.POSITIVE_INFINITY: Represents positive infinity.
  • 5.
    Number Methods Methods arefunctions associated with an object that perform actions or operations related to the object. They are invoked using parentheses.
  • 6.
    Some Methods ofNumber 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"
  • 7.
    toLocaleString(): Returns astring 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"
  • 8.
    toString(): Returns astring 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
  • 9.
    JAVASCRIPT BOOLEAN In JavaScript, booleanis 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.
  • 10.
    Boolean Methods toSource(): Returnsa 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.
  • 11.
    JAVASCRIPT STRING In JavaScript, theString 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.
  • 12.
    String Properties Length: Returnsthe length of the string.
  • 13.
    String Methods charAt(): Returnsthe 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")
  • 14.
    concat(): Combines twoor 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
  • 15.
    lastIndexOf(): Returns theindex 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!"
  • 16.
    search(): Searches astring 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"
  • 17.
    split(): Splits astring 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"