Home > Article > Web Front-end > A deep dive into the capabilities and features of JavaScript’s built-in objects
In-depth analysis of the functions and characteristics of JS built-in objects
JavaScript is an object-based programming language. It provides many built-in objects, which have various rich features. functions and features. In this article, we will provide an in-depth analysis of some commonly used built-in objects and give corresponding code examples.
// 求绝对值 Math.abs(-10); // 输出:10 // 向上取整 Math.ceil(3.14); // 输出:4 // 向下取整 Math.floor(3.14); // 输出:3 // 求最大值 Math.max(1, 2, 3); // 输出:3 // 求最小值 Math.min(1, 2, 3); // 输出:1 // 生成一个0到1之间的随机数 Math.random(); // 输出:0.20793662077218673
// 字符串长度 var str = "Hello World"; str.length; // 输出:11 // 查找子串 str.indexOf("World"); // 输出:6 // 截取子串 str.slice(0, 5); // 输出:Hello // 替换子串 str.replace("World", "JavaScript"); // 输出:Hello JavaScript // 字符串转换为大写 str.toUpperCase(); // 输出:HELLO WORLD
// 创建一个新数组 var arr = new Array(1, 2, 3); // 获取数组长度 arr.length; // 输出:3 // 插入元素 arr.push(4); // [1, 2, 3, 4] // 删除并返回最后一个元素 arr.pop(); // 输出:4 // 循环遍历数组 for (var i = 0; i < arr.length; i++) { console.log(arr[i]); } // 数组元素排序 arr.sort(); // [1, 2, 3]
// 获取当前日期和时间 var now = new Date(); console.log(now); // 输出:Sat Dec 18 2021 14:16:38 GMT+0800 (中国标准时间) // 获取年份 now.getFullYear(); // 输出:2021 // 获取月份(0表示1月,11表示12月) now.getMonth(); // 输出:11 // 获取日期 now.getDate(); // 输出:18 // 获取小时 now.getHours(); // 输出:14 // 获取分钟 now.getMinutes(); // 输出:16
Through an in-depth analysis of the built-in objects in JavaScript, we can understand the rich functions and features they provide, and be able to use them more flexibly. to deal with various issues. I hope this article helps you understand JavaScript built-in objects!
The above is the detailed content of A deep dive into the capabilities and features of JavaScript’s built-in objects. For more information, please follow other related articles on the PHP Chinese website!