Javascript methods
Created By: chatGPT
JavaScript provides a plethora of built-in methods that can be used to perform a variety of operations. Here are a few commonly used methods: String methods, Array methods, and Object methods.String methods are essential for working with text data. Some of the most commonly used string methods include:Array methods allow you to manipulate arrays effectively. Key array methods include:Object methods are used to manipulate objects in JavaScript. Here are a couple of important ones:Understanding these core methods will greatly enhance your ability to write efficient JavaScript code and manipulate data effectively.
toUpperCase()
: Converts a string to uppercase.
const text = 'hello';
const upperText = text.toUpperCase();
console.log(upperText); // Output: HELLO
toLowerCase()
: Converts a string to lowercase.
const text = 'HELLO';
const lowerText = text.toLowerCase();
console.log(lowerText); // Output: hello
trim()
: Removes whitespace from both ends of a string.
const text = ' hello ';
const trimmedText = text.trim();
console.log(trimmedText); // Output: 'hello'
push()
: Adds one or more elements to the end of an array.
const arr = [1, 2, 3];
arr.push(4);
console.log(arr); // Output: [1, 2, 3, 4]
pop()
: Removes the last element from an array.
const arr = [1, 2, 3, 4];
const lastElement = arr.pop();
console.log(lastElement); // Output: 4
map()
: Creates a new array with the results of calling a provided function on every element in the calling array.
const arr = [1, 2, 3];
const doubled = arr.map(x => x * 2);
console.log(doubled); // Output: [2, 4, 6]
Object.keys()
: Returns an array of a given object's property names.
const obj = { a: 1, b: 2, c: 3 };
const keys = Object.keys(obj);
console.log(keys); // Output: ['a', 'b', 'c']
Object.values()
: Returns an array of a given object's property values.
const obj = { a: 1, b: 2, c: 3 };
const values = Object.values(obj);
console.log(values); // Output: [1, 2, 3]