Title: Common methods and code examples of JavaScript built-in objects
Abstract: JavaScript, as a powerful scripting language, often uses built-in objects in development. This article will introduce commonly used built-in objects and their methods in JavaScript, and provide corresponding code examples.
1. String object (String)
charAt()
Description: Get the character at the specified position in the string according to the index.
Sample code:
let str = "Hello World!"; console.log(str.charAt(0)); // 输出:H
concat()
Description: Used to concatenate two or more strings.
Sample code:
let str1 = "Hello"; let str2 = "World"; console.log(str1.concat(" ", str2)); // 输出:Hello World
indexOf()
Description: Returns the first matching index value of the specified substring in the string.
Sample code:
let str = "Hello World!"; console.log(str.indexOf("o")); // 输出:4
2. Numeric object (Number)
toFixed()
Description: Convert the number to A string specifying the number of decimal places.
Sample code:
let num = 3.1415926; console.log(num.toFixed(2)); // 输出:3.14
toString()
Description: Convert a number to a string.
Sample code:
let num = 123; console.log(num.toString()); // 输出:"123"
isNaN()
Description: Determine whether a value is NaN (non-numeric value).
Sample code:
console.log(isNaN("Hello")); // 输出:true console.log(isNaN(123)); // 输出:false
3. Array object (Array)
push()
Description: Add to the end of the array One or more elements and returns the length of the array after adding new elements.
Sample code:
let fruits = ["apple", "banana"]; console.log(fruits.push("orange")); // 输出:3 console.log(fruits); // 输出:["apple", "banana", "orange"]
pop()
Description: Remove the last element of the array and return the element.
Sample code:
let fruits = ["apple", "banana", "orange"]; console.log(fruits.pop()); // 输出:"orange" console.log(fruits); // 输出:["apple", "banana"]
join()
Description: Concatenate array elements into strings, separated by the specified delimiter.
Sample code:
let fruits = ["apple", "banana", "orange"]; console.log(fruits.join(", ")); // 输出:"apple, banana, orange"
The above are just some of the commonly used methods of JavaScript’s built-in objects. There are many other useful methods that are not listed one by one. In order to better use JavaScript built-in objects, it is recommended to view the relevant documentation and conduct more in-depth study and practice.
The above is the detailed content of What are the methods of built-in objects in js?. For more information, please follow other related articles on the PHP Chinese website!