Built In Functions - CSU677 - Shoolini U

Built In Functions

1. JavaScript Built-in Functions

JavaScript provides a variety of built-in functions for common tasks, such as manipulating data types, working with strings, arrays, numbers, and more. Below are 40 key functions, grouped into categories for better understanding.

1.1 String Functions

1.1.1 charAt()

Returns the character at a specified index in a string.

let str = "Hello";
console.log(str.charAt(0)); // Output: "H"
1.1.2 concat()

Joins two or more strings and returns a new string.

let str1 = "Hello";
let str2 = "World";
console.log(str1.concat(" ", str2)); // Output: "Hello World"
1.1.3 includes()

Checks if a string contains a specified substring.

let str = "Hello World";
console.log(str.includes("World")); // Output: true
1.1.4 indexOf()

Returns the index of the first occurrence of a specified substring.

let str = "Hello World";
console.log(str.indexOf("World")); // Output: 6
1.1.5 slice()

Extracts a section of a string and returns it as a new string.

let str = "Hello World";
console.log(str.slice(0, 5)); // Output: "Hello"
1.1.6 split()

Splits a string into an array of substrings.

let str = "Hello World";
console.log(str.split(" ")); // Output: ["Hello", "World"]
1.1.7 substring()

Extracts a portion of a string between two specified indices.

let str = "Hello World";
console.log(str.substring(0, 5)); // Output: "Hello"
1.1.8 toLowerCase()

Converts a string to lowercase.

let str = "Hello World";
console.log(str.toLowerCase()); // Output: "hello world"
1.1.9 toUpperCase()

Converts a string to uppercase.

let str = "Hello World";
console.log(str.toUpperCase()); // Output: "HELLO WORLD"
1.1.10 trim()

Removes whitespace from both sides of a string.

let str = "  Hello World  ";
console.log(str.trim()); // Output: "Hello World"

1.2 Array Functions

1.2.1 push()

Adds one or more elements to the end of an array.

let arr = [1, 2, 3];
arr.push(4);
console.log(arr); // Output: [1, 2, 3, 4]
1.2.2 pop()

Removes the last element from an array.

let arr = [1, 2, 3];
arr.pop();
console.log(arr); // Output: [1, 2]
1.2.3 shift()

Removes the first element from an array.

let arr = [1, 2, 3];
arr.shift();
console.log(arr); // Output: [2, 3]
1.2.4 unshift()

Adds one or more elements to the beginning of an array.

let arr = [1, 2, 3];
arr.unshift(0);
console.log(arr); // Output: [0, 1, 2, 3]
1.2.5 join()

Joins all elements of an array into a string.

let arr = ["Hello", "World"];
console.log(arr.join(" ")); // Output: "Hello World"
1.2.6 reverse()

Reverses the elements of an array.

let arr = [1, 2, 3];
arr.reverse();
console.log(arr); // Output: [3, 2, 1]
1.2.7 slice()

Returns a shallow copy of a portion of an array into a new array.

let arr = [1, 2, 3, 4];
console.log(arr.slice(1, 3)); // Output: [2, 3]
1.2.8 splice()

Adds/removes items to/from an array.

let arr = [1, 2, 3];
arr.splice(1, 1);
console.log(arr); // Output: [1, 3]
1.2.9 map()

Creates a new array populated with the results of calling a function on every element in the array.

let arr = [1, 2, 3];
let newArr = arr.map(x => x * 2);
console.log(newArr); // Output: [2, 4, 6]
1.2.10 filter()

Creates a new array with all elements that pass a test.

let arr = [1, 2, 3, 4];
let filtered = arr.filter(x => x > 2);
console.log(filtered); // Output: [3, 4]

1.3 Number Functions

1.3.1 toFixed()

Formats a number to a specified number of decimal places.

let num = 3.14159;
console.log(num.toFixed(2)); // Output: "3.14"
1.3.2 toPrecision()

Formats a number to a specified length.

let num = 3.14159;
console.log(num.toPrecision(3)); // Output: "3.14"
1.3.3 parseInt()

Parses a string and returns an integer.

let str = "42";
console.log(parseInt(str)); // Output: 42
1.3.4 parseFloat()

Parses a string and returns a floating-point number.

let str = "3.14";
console.log(parseFloat(str)); // Output: 3.14
1.3.5 isNaN()

Checks if a value is NaN (Not-a-Number).

console.log(isNaN("Hello")); // Output: true
1.3.6 Math.random()

Returns a random number between 0 (inclusive) and 1 (exclusive).

console.log(Math.random());
1.3.7 Math.round()

Rounds a number to the nearest integer.

console.log(Math.round(3.5)); // Output: 4
1.3.8 Math.floor()

Rounds a number down to the nearest integer.

console.log(Math.floor(3.9)); // Output: 3
1.3.9 Math.ceil()

Rounds a number up to the nearest integer.

console.log(Math.ceil(3.1)); // Output: 4
1.3.10 Math.abs()

Returns the absolute value of a number.

console.log(Math.abs(-4)); // Output: 4

1.4 Object Functions

1.4.1 Object.keys()

Returns an array of a given object's own enumerable property names.

let obj = {a: 1, b: 2};
console.log(Object.keys(obj)); // Output: ["a", "b"]
1.4.2 Object.values()

Returns an array of a given object's own enumerable property values.

let obj = {a: 1, b: 2};
console.log(Object.values(obj)); // Output: [1, 2]
1.4.3 Object.entries()

Returns an array of a given object's own enumerable property key-value pairs.

let obj = {a: 1, b: 2};
console.log(Object.entries(obj)); // Output: [["a", 1], ["b", 2]]
1.4.4 Object.assign()

Copies all enumerable own properties from one or more source objects to a target object.

let target = {};
let source = {a: 1, b: 2};
Object.assign(target, source);
console.log(target); // Output: {a: 1, b: 2}
1.4.5 Object.freeze()

Freezes an object, preventing new properties from being added or removed.

let obj = {a: 1};
Object.freeze(obj);
obj.a = 2; // This will not work
console.log(obj); // Output: {a: 1}