Common JavaScript Methods
JavaScript is a powerful and versatile programming language widely used for web development. It provides numerous methods and functionalities to manipulate data, interact with users, and enhance web applications. In this guide, we will explore 23 essential JavaScript methods, each with a detailed explanation and example code. These methods are crucial for everyday tasks and will help you write cleaner, more efficient code. Whether you are a beginner or an experienced developer, understanding these methods will improve your coding skills and overall productivity.
// Removes the last element from an array and returns it.
let arr = [1, 2, 3];
arr.pop(); // arr: [1, 2]
// Adds a new element to the end of an array.
let arr = [1, 2, 3];
arr.push(4); // arr: [1, 2, 3, 4]
// Sorts the elements of an array in ascending order.
let arr = [3, 1, 2];
arr.sort(); // arr: [1, 2, 3]
// Reverses the order of elements in an array.
let arr = [1, 2, 3];
arr.reverse(); // arr: [3, 2, 1]
// Replaces the first occurrence of a specified substring in a string with another substring.
let str = "JavaScript";
str = str.replace("Script", "Script!"); // str: "JavaScript!"
// Returns the number of characters in a string.
let str = "JavaScript";
let length = str.length; // length: 10
// Converts all characters in a string to lowercase.
let str = "JavaScript";
str = str.toLowerCase(); // str: "javascript"
// Converts all characters in a string to uppercase.
let str = "JavaScript";
str = str.toUpperCase(); // str: "JAVASCRIPT"
// Rounds a number to the nearest integer.
let num = 4.7;
num = Math.round(num); // num: 5
// Rounds a number down to the nearest integer.
let num = 4.7;
num = Math.floor(num); // num: 4
// Rounds a number up to the nearest integer.
let num = 4.3;
num = Math.ceil(num); // num: 5
// Returns a pseudo-random number between 0 (inclusive) and 1 (exclusive).
let num = Math.random(); // num: a number between 0 and 1
// Checks if a specific key exists in an object.
let obj = {name: 'John'};
let hasName = 'name' in obj; // hasName: true
// Checks if an object has a specific key as its own property.
let obj = {name: 'John'};
let hasName = obj.hasOwnProperty('name'); // hasName: true
// Tests whether all elements in an array pass the test implemented by the provided function.
let arr = [1, 2, 3];
let allPositive = arr.every(num => num > 0); // allPositive: true
// Returns the first element in an array that satisfies the provided testing function.
let arr = [5, 12, 8];
let found = arr.find(num => num > 10); // found: 12
// Creates a new array with all elements that pass the test implemented by the provided function.
let arr = [5, 12, 8];
let filtered = arr.filter(num => num > 10); // filtered: [12]
// Joins all elements of an array into a single string, separated by the specified separator.
let arr = ['a', 'b', 'c'];
let str = arr.join('-'); // str: "a-b-c"
// Merges two or more arrays into one array.
let arr1 = [1, 2];
let arr2 = [3, 4];
let newArr = arr1.concat(arr2); // newArr: [1, 2, 3, 4]
// Executes a provided function once for each array element.
let arr = [1, 2, 3];
arr.forEach(num => console.log(num)); // Outputs: 1 2 3
// Creates a new Date object representing the current date and time.
let date = new Date(); // Represents the current date and time
// Retrieves the current year from a Date object.
let date = new Date();
let year = date.getFullYear(); // year: Current year
// Retrieves the current month from a Date object (0-indexed, so January is 0).
let date = new Date();
let month = date.getMonth(); // month: 0 for January, 1 for February, etc.
We hope this guide has been helpful in demonstrating some of the most commonly used JavaScript methods. These methods are foundational for effective programming in JavaScript and can greatly simplify your development process. Practice using these methods in your projects to become more proficient in JavaScript. Happy coding!