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?

王林
Release: 2024-02-18 15:42:06
Original
698 people have browsed it

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
    Copy after login
  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
    Copy after login
  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
    Copy after login

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

    let num = 123;
    console.log(num.toString());  // 输出:"123"
    Copy after login
  3. isNaN()
    Description: Determine whether a value is NaN (non-numeric value).
    Sample code:

    console.log(isNaN("Hello"));  // 输出:true
    console.log(isNaN(123));      // 输出:false
    Copy after login

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"]
    Copy after login
  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"]
    Copy after login
  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"
    Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template