JavaScript Built-in functions and how they works: -

🌐 Tech Enthusiast | Curious Learner | Knowledge Sharer Hello! Currently, I am pursuing a Master's degree in Computer Applications (MCA) from the Heritage Institute of Technology. Since the world of technology is dynamic, I posted some pieces based on my experiences and what is trending right now. My blog is an informative space where it is possible to create a community, learn together, and grow. Considering my interests in Web Development, Artificial Intelligence, and DevOps, I am always ready to explore new technologies, share knowledge with others, and step up the ladder of success simultaneously. Join me as I navigate this exciting journey of growth and innovation! 🚀
JavaScript has several "top-level" built-in functions. JavaScript also has four built-in objects: Array, Date, Math, and String. Each object has special-purpose properties and methods associated with it. JavaScript also has constructors for Boolean and Number types.
In this blog we take a look in that pool of Built-in function:
Here's a categorized list of commonly used JavaScript built-in functions based on their primary use with strings, arrays, numbers, and objects:
String Functions
charAt()Purpose: Returns the character at a specified index in a string.
Syntax:
string.charAt(index)Example:
"Hello".charAt(1); // returns 'e'
concat()Purpose: Concatenates two or more strings.
Syntax:
string1.concat(string2, string3, ...)Example:
"Hello".concat(" World"); // returns 'Hello World'
includes()Purpose: Determines whether a string contains a specified value.
Syntax:
string.includes(substring)Example:
"Hello World".includes("World"); // returns true
indexOf()Purpose: Returns the index of the first occurrence of a specified value in a string.
Syntax:
string.indexOf(substring)Example:
"Hello World".indexOf("World"); // returns 6
replace()Purpose: Replaces a substring with a new substring.
Syntax:
string.replace(oldValue, newValue)Example:
"Hello World".replace("World", "Universe"); // returns 'Hello Universe'
split()Purpose: Splits a string into an array of substrings.
Syntax:
string.split(separator)Example:
"Hello World".split(" "); // returns ['Hello', 'World']
toLowerCase()Purpose: Converts a string to lowercase.
Syntax:
string.toLowerCase()Example:
"HELLO".toLowerCase(); // returns 'hello'
toUpperCase()Purpose: Converts a string to uppercase.
Syntax:
string.toUpperCase()Example:
"hello".toUpperCase(); // returns 'HELLO'
trim()Purpose: Removes whitespace from both ends of a string.
Syntax:
string.trim()Example:
" Hello ".trim(); // returns 'Hello'
Array Functions
push()Purpose: Adds one or more elements to the end of an array.
Syntax:
array.push(element1, element2, ...)Example:
let arr = [1, 2]; arr.push(3); // arr becomes [1, 2, 3]
pop()Purpose: Removes the last element from an array and returns it.
Syntax:
array.pop()Example:
let arr = [1, 2, 3]; arr.pop(); // returns 3, arr becomes [1, 2]
shift()Purpose: Removes the first element from an array and returns it.
Syntax:
array.shift()Example:
let arr = [1, 2, 3]; arr.shift(); // returns 1, arr becomes [2, 3]
unshift()Purpose: Adds one or more elements to the beginning of an array.
Syntax:
array.unshift(element1, element2, ...)Example:
let arr = [2, 3]; arr.unshift(1); // arr becomes [1, 2, 3]
slice()Purpose: Returns a shallow copy of a portion of an array.
Syntax:
array.slice(start, end)Example:
let arr = [1, 2, 3, 4]; arr.slice(1, 3); // returns [2, 3]
splice()Purpose: Adds or removes elements from an array.
Syntax:
array.splice(start, deleteCount, item1, item2, ...)Example:
let arr = [1, 2, 3, 4]; arr.splice(1, 2); // removes 2 elements from index 1, arr becomes [1, 4]
forEach()Purpose: Executes a function for each element in an array.
Syntax:
array.forEach(callback)Example:
[1, 2, 3].forEach(num => console.log(num)); // logs 1, 2, 3
map()Purpose: Creates a new array with the results of calling a function on every element in the array.
Syntax:
array.map(callback)Example:
let arr = [1, 2, 3]; let newArr = arr.map(num => num * 2); // returns [2, 4, 6]
filter()Purpose: Creates a new array with all elements that pass the test implemented by the provided function.
Syntax:
array.filter(callback)Example:
let arr = [1, 2, 3, 4]; let even = arr.filter(num => num % 2 === 0); // returns [2, 4]
reduce()Purpose: Executes a reducer function on each element of the array, resulting in a single output value.
Syntax:
array.reduce(callback, initialValue)Example:
let arr = [1, 2, 3, 4]; let sum = arr.reduce((acc, num) => acc + num, 0); // returns 10
Number Functions
parseInt()Purpose: Parses a string and returns an integer.
Syntax:
parseInt(string, radix)Example:
parseInt("10"); // returns 10
parseFloat()Purpose: Parses a string and returns a floating-point number.
Syntax:
parseFloat(string)Example:
parseFloat("10.5"); // returns 10.5
toFixed()Purpose: Formats a number to a specified number of decimal places.
Syntax:
number.toFixed(decimalPlaces)Example:
let num = 10.1234; num.toFixed(2); // returns '10.12'
toString()Purpose: Converts a number to a string.
Syntax:
number.toString()Example:
let num = 10; num.toString(); // returns '10'
Object Functions
Object.keys()Purpose: Returns an array of a given object's own property names.
Syntax:
Object.keys(object)Example:
let obj = { a: 1, b: 2 }; Object.keys(obj); // returns ['a', 'b']
Object.values()Purpose: Returns an array of a given object's own property values.
Syntax:
Object.values(object)Example:
let obj = { a: 1, b: 2 }; Object.values(obj); // returns [1, 2]
Object.entries()Purpose: Returns an array of a given object's own enumerable property
[key, value]pairs.Syntax:
Object.entries(object)Example:
let obj = { a: 1, b: 2 }; Object.entries(obj); // returns [['a', 1], ['b', 2]]
Object.assign()Purpose: Copies all enumerable own properties from one or more source objects to a target object.
Syntax:
Object.assign(target, ...sources)Example:
let target = { a: 1 }; let source = { b: 2 }; Object.assign(target, source); // target becomes { a: 1, b: 2 }
In this blog we discuss very commonly used built-in functions in JavaScript . This list covers some of the most frequently used JavaScript functions categorized by their use in strings, arrays, numbers, and objects.
Thank You ,
Happy reading.