Home >Web Front-end >JS Tutorial >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.
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:
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(随机数)
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:
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(时间戳)
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:
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' ]
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:
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!