// Common Builtin JavaScript Objects
String Objects
Multi-line Strings: You can create strings that span multiple lines.
const multiLine = `This is
a multi-line string.`;
String Interpolation: You can embed expressions directly inside the string
using ${}.
const name = 'John';
const greeting = `Hello, ${name}!`;
// Output: Hello, John!
Expression Embedding: Allows you to include any JavaScript expression.
const a = 5;
const b = 10;
const result = `The sum is ${a + b}`;
// Output: The sum is 15
So, backticks make string manipulation easier and more readable.
In JavaScript, there are several common String object methods and properties
that are
used for manipulating and handling strings:
1. Properties
length: Returns the length of the string.
const str = "Hello, World!";
console.log(str.length); // Output: 13
2. Methods
charAt(index): Returns the character at the specified index.
const str = "Hello";
console.log(str.charAt(1)); // Output: 'e'
toUpperCase(): Converts the entire string to uppercase.
const str = "hello";
console.log(str.toUpperCase()); // Output: 'HELLO'
toLowerCase(): Converts the entire string to lowercase.
const str = "HELLO";
console.log(str.toLowerCase()); // Output: 'hello'
includes(substring): Checks if the string contains the specified substring.
const str = "Hello, World!";
console.log(str.includes("World")); // Output: true
indexOf(substring): Returns the index of the first occurrence of the specified
substring, or -1 if not found.
const str = "Hello, World!";
console.log(str.indexOf("World")); // Output: 7
slice(start, end): Extracts a section of the string from start to end (not
including end).
const str = "Hello, World!";
console.log(str.slice(0, 5)); // Output: 'Hello'
substring(start, end): Similar to slice but does not accept negative indexes.
const str = "Hello, World!";
console.log(str.substring(0, 5)); // Output: 'Hello'
replace(search, newValue): Replaces the first occurrence of search with
newValue.
const str = "Hello, World!";
console.log(str.replace("World", "JavaScript")); // Output: 'Hello, JavaScript!'
split(separator): Splits the string into an array based on the specified
separator.
const str = "Hello, World!";
console.log(str.split(", ")); // Output: ['Hello', 'World!']
trim(): Removes whitespace from both ends of the string.
const str = " Hello, World! ";
console.log(str.trim()); // Output: 'Hello, World!'
These methods and properties are essential for common string operations in
JavaScript.
// Common Builtin Array objects
Properties
length: Number of elements in an array.
arr.length
Methods
push(element): Add to the end.
pop(): Remove from the end.
shift(): Remove from the beginning.
unshift(element): Add to the beginning.
indexOf(element): Find first index.
includes(element): Check if exists.
slice(start, end): Copy a portion.
splice(start, deleteCount, items): Modify array in place.
join(separator): Join into a string.
concat(array): Merge arrays.
forEach(callback): Loop through elements.
map(callback): Transform each element.
filter(callback): Filter elements.
find(callback): Find first match.
reduce(callback, initialValue): Accumulate to a single value.
This should cover the most commonly used methods and properties!
example
const fruits = ['Apple', 'Banana'];
// Using push to add an element to the end
fruits.push('Orange');
console.log(fruits); // Output: ['Apple', 'Banana', 'Orange']
// Using pop to remove the last element
const lastFruit = fruits.pop();
console.log(lastFruit); // Output: 'Orange'
console.log(fruits); // Output: ['Apple', 'Banana']
// Built in Math Objects in Javascript
Properties
Math.PI: The value of π (3.14159...).
Math.PI // Output: 3.141592653589793
Methods
Math.abs(x): Absolute value.
Math.ceil(x): Rounds up to the nearest integer.
Math.floor(x): Rounds down to the nearest integer.
Math.round(x): Rounds to the nearest integer.
Math.max(a, b, ...): Highest number.
Math.min(a, b, ...): Lowest number.
Math.random(): Random number between 0 (inclusive) and 1 (exclusive).
Math.sqrt(x): Square root.
Math.pow(x, y): x raised to the power of y.
Math.trunc(x): Remove decimal part (truncate).
example
const number = 16;
const squareRoot = Math.sqrt(number);
console.log(`The square root of ${number} is ${squareRoot}`);