Home >Web Front-end >JS Tutorial >What are the methods of built-in objects in js?

What are the methods of built-in objects in js?

王林
王林Original
2024-02-18 15:42:06721browse

What are the methods of built-in objects in js?

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)

  1. 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
  2. concat()
    Description: Used to concatenate two or more strings.
    Sample code:

    let str1 = "Hello";
    let str2 = "World";
    console.log(str1.concat(" ", str2));  // 输出:Hello World
  3. 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)

  1. 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
  2. toString()
    Description: Convert a number to a string.
    Sample code:

    let num = 123;
    console.log(num.toString());  // 输出:"123"
  3. 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)

  1. 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"]
  2. 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"]
  3. 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn