To master the common usage of JS built-in objects, specific code examples are required
When developing using JavaScript, we often deal with various built-in objects. These built-in objects provide a wealth of functions and methods that can help us implement various complex logic and operations. Learning to master the common usage of these built-in objects can improve our coding efficiency and development quality. This article will introduce several common JS built-in objects and give specific code examples.
The Math object provides many mathematics-related methods, such as logarithms, trigonometric functions, exponential operations, etc. Here are some commonly used sample codes:
// 获取一个随机数 let randomNum = Math.random(); console.log(randomNum); // 求绝对值 let absNum = Math.abs(-10); console.log(absNum); // 求平方根 let sqrtNum = Math.sqrt(16); console.log(sqrtNum); // 求最大值 let maxNum = Math.max(1, 2, 3); console.log(maxNum); // 求最小值 let minNum = Math.min(1, 2, 3); console.log(minNum);
The Date object is used to handle dates and times. It provides many methods, such as getting the current time, setting the time, formatting output, etc. Here are a few commonly used code examples:
// 获取当前时间 let currentDate = new Date(); console.log(currentDate); // 获取年份 let year = currentDate.getFullYear(); console.log(year); // 获取月份(从0开始计数,0代表一月) let month = currentDate.getMonth(); console.log(month); // 获取日期 let day = currentDate.getDate(); console.log(day); // 设置日期 currentDate.setDate(10); console.log(currentDate); // 格式化输出日期 let formattedDate = currentDate.toLocaleDateString(); console.log(formattedDate);
String object is used to process strings. It provides many methods, such as getting the string length, finding substrings, replacing strings, etc. Here are a few commonly used code examples:
// 获取字符串长度 let str = "Hello, world!"; let length = str.length; console.log(length); // 查找子串 let index = str.indexOf("world"); console.log(index); // 替换字符串 let newStr = str.replace("world", "JavaScript"); console.log(newStr); // 字符串分割 let splittedStr = str.split(","); console.log(splittedStr);
The Array object is used to process arrays. It provides many methods, such as adding elements, deleting elements, traversing arrays, etc. The following are several commonly used code examples:
// 创建一个空数组 let arr = []; // 添加元素 arr.push(1); arr.push(2); arr.push(3); console.log(arr); // 删除元素 arr.pop(); console.log(arr); // 数组遍历 arr.forEach(function(element) { console.log(element); });
The above is only a brief introduction and sample code for several common built-in objects. In actual development, there are many other built-in objects available for use, such as RegExp objects, JSON objects, etc. Through continuous learning and practice, we can master the usage of these built-in objects more deeply and improve our programming level.
The above is the detailed content of Common Usage Revealed: Mastering JS Built-in Objects. For more information, please follow other related articles on the PHP Chinese website!