JavaScript provides a variety of built-in methods that can be used on different data types. Here are some commonly used built-in methods along with brief descriptions of the values they return:
String Methods:-
length:- Returns the number of characters in a string.
let str = "Hello, World!"; console.log(str.length); // Output: 13 -
toUpperCase():- Returns a new string with all characters converted to uppercase.
let str = "hello"; console.log(str.toUpperCase()); // Output: "HELLO" -
toLowerCase():- Returns a new string with all characters converted to lowercase.
let str = "HELLO"; console.log(str.toLowerCase()); // Output: "hello" -
substring(start, end):- Returns a substring of the original string based on the specified start and end indices.
let str = "Hello, World!"; console.log(str.substring(0, 5)); // Output: "Hello"
-
length:- Returns the number of elements in an array.
let arr = [1, 2, 3, 4, 5]; console.log(arr.length); // Output: 5 -
push(element):- Adds an element to the end of an array and returns the new length of the array.
let arr = [1, 2, 3]; console.log(arr.push(4)); // Output: 4 (new length) -
pop():- Removes the last element from an array and returns that element.
let arr = [1, 2, 3, 4]; console.log(arr.pop()); // Output: 4 (removed element) -
join(separator):- Joins all elements of an array into a string, separated by the specified separator.
let arr = ["apple", "orange", "banana"]; console.log(arr.join(", ")); // Output: "apple, orange, banana" -
indexOf(element):- Returns the index of the first occurrence of an element in an array. Returns -1 if the element is not found.
let arr = [10, 20, 30, 40, 50]; console.log(arr.indexOf(30)); // Output: 2
These are just a few examples, and JavaScript provides many more built-in methods for various data types. The returned values depend on the specific method used.