Home  >  Article  >  Web Front-end  >  Functions and usage of js built-in objects

Functions and usage of js built-in objects

王林
王林Original
2024-01-10 10:59:451171browse

Functions and usage of js built-in objects

Function and usage of js built-in objects

JavaScript (JS for short) is a scripting language that is widely used in Web development. As an object-oriented language, JS provides many built-in objects that contain various functions and methods to help developers program more efficiently.

This article will introduce some commonly used JS built-in objects, including Math, Date, Array and String, as well as their functions and usage, and provide specific code examples.

  1. Math object

The Math object is a math-related built-in object that provides many commonly used mathematical calculation methods.

The common methods of Math objects are as follows:

  • Math.abs(x): Returns the absolute value of x.
  • Math.ceil(x): Returns the smallest integer greater than or equal to x.
  • Math.floor(x): Returns the largest integer less than or equal to x.
  • Math.round(x): Returns the integer closest to x.
  • Math.max(x1, x2, ...): Returns the maximum value in a set of numbers.
  • Math.min(x1, x2, ...): Returns the minimum value in a set of numbers.
  • Math.random(): Returns a random number between 0 and 1.

Sample code:

console.log(Math.abs(-5));  // 输出:5
console.log(Math.ceil(3.7));  // 输出:4
console.log(Math.floor(3.7));  // 输出:3
console.log(Math.round(3.2));  // 输出:3
console.log(Math.max(1, 2, 3));  // 输出:3
console.log(Math.min(1, 2, 3));  // 输出:1
console.log(Math.random());  // 输出:0.123456789(随机数)
  1. Date object

The Date object is used to handle date and time related operations. You can create a Date object that represents the current time, or you can create a Date object by specifying the date and time.

The common methods of Date objects are as follows:

  • new Date(): Create a Date object representing the current time.
  • new Date(year, month, day [, hours, minutes, seconds, milliseconds]): Create a Date object specifying the date and time.
  • Date.now(): Returns the timestamp of the current time.

Sample code:

var now = new Date();
console.log(now);  // 输出:Mon Dec 20 2021 15:30:00 GMT+0800 (中国标准时间)

var specificDate = new Date(2021, 11, 25);
console.log(specificDate);  // 输出:Fri Dec 25 2021 00:00:00 GMT+0800 (中国标准时间)

var timestamp = Date.now();
console.log(timestamp);  // 输出:1639977000000(时间戳)
  1. Array object

The Array object is a built-in object used to store and manipulate a set of data. An Array object can be created through a literal or constructor.

The common methods of Array objects are as follows:

  • push(element1, element2, ...): Add one or more elements to the end of the array.
  • pop(): Remove and return the last element of the array.
  • shift(): Delete and return the first element of the array.
  • unshift(element1, element2, ...): Add one or more elements to the beginning of the array.
  • concat(array1, array2, ...): Returns a new array that merges multiple arrays.

Sample code:

var fruits = ['apple', 'banana', 'orange'];
fruits.push('pear');
console.log(fruits);  // 输出:[ 'apple', 'banana', 'orange', 'pear' ]

var lastFruit = fruits.pop();
console.log(lastFruit);  // 输出:pear
console.log(fruits);  // 输出:[ 'apple', 'banana', 'orange' ]

var firstFruit = fruits.shift();
console.log(firstFruit);  // 输出:apple
console.log(fruits);  // 输出:[ 'banana', 'orange' ]

fruits.unshift('grape');
console.log(fruits);  // 输出:[ 'grape', 'banana', 'orange' ]

var moreFruits = ['watermelon', 'kiwi'];
var allFruits = fruits.concat(moreFruits);
console.log(allFruits);  // 输出:[ 'grape', 'banana', 'orange', 'watermelon', 'kiwi' ]
  1. String object

String object is a built-in object used for processing strings. You can use the methods of the String object to operate on strings, such as concatenation, search, and replacement.

The common methods of String objects are as follows:

  • length: Returns the length of the string.
  • charAt(index): Returns the character at the specified index position.
  • indexOf(substring): Returns the index position of the first occurrence of the substring in the string.
  • substring(startIndex, endIndex): Returns the substring of the specified range in the string.
  • replace(oldValue, newValue): Replace the old value in the string with the new value.

Sample code:

var message = 'Hello, World!';
console.log(message.length);  // 输出:13

console.log(message.charAt(4));  // 输出:o

console.log(message.indexOf('World'));  // 输出:7

console.log(message.substring(7, 12));  // 输出:World

var newMessage = message.replace('World', 'JavaScript');
console.log(newMessage);  // 输出:Hello, JavaScript!

The above is an introduction to some commonly used JS built-in objects and their functions and usage. Through the methods of these built-in objects, we can process mathematical operations more conveniently , date and time, array and string operations. Proficiency in these objects and their methods can greatly improve the efficiency and quality of JS programming.

The above is the detailed content of Functions and usage of js built-in objects. 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