Home  >  Article  >  Web Front-end  >  Master common JS built-in objects

Master common JS built-in objects

王林
王林Original
2024-01-13 12:39:06859browse

Master common JS built-in objects

To understand the commonly used built-in objects of JS, you need specific code examples

JavaScript (JS for short) is a scripting language that is widely used in web development and other fields. In JS, built-in objects refer to objects that have been defined within the language and can be directly called and used by developers. It is very necessary for developers to be familiar with and understand the commonly used built-in objects in JS. This article will introduce some commonly used JS built-in objects and give corresponding code examples.

  1. Math object
    The Math object provides a large number of mathematical operation methods and constants. Through this object, you can perform various advanced mathematical calculations, such as calculating square roots, trigonometric functions, etc.

Sample code:

// 计算平方根
var number = Math.sqrt(16);
console.log(number);  // 输出4

// 计算绝对值
var absoluteValue = Math.abs(-5);
console.log(absoluteValue);  // 输出5

// 计算随机数
var randomNum = Math.random();  
console.log(randomNum);  // 输出0-1之间的随机数
  1. Date object
    Date object is used to process dates and times. It provides methods to obtain year, month, day, hour, minute, second and other information, and can format the date.

Sample code:

// 获取当前日期和时间
var currentDate = new Date();
console.log(currentDate);  // 输出当前日期和时间

// 获取当前年份
var currentYear = currentDate.getFullYear();
console.log(currentYear);  // 输出当前年份

// 格式化日期
var formattedDate = currentDate.toLocaleDateString();
console.log(formattedDate);  // 输出格式化后的日期
  1. String object
    String object is used to process strings. It provides many string-related methods, such as getting the length of a string, intercepting substrings, converting case, etc.

Sample code:

// 获取字符串长度
var str = "Hello World";
var length = str.length;
console.log(length);  // 输出11

// 截取子字符串
var subStr = str.substring(0, 5);
console.log(subStr);  // 输出Hello

// 转换为大写
var uppercaseStr = str.toUpperCase();
console.log(uppercaseStr);  // 输出HELLO WORLD
  1. Array object
    The Array object is used to process arrays. It provides methods for adding, deleting, modifying and checking arrays, such as adding elements, deleting elements, traversing arrays, etc.

Sample code:

// 定义数组
var arr = [1, 2, 3, 4, 5];

// 添加元素
arr.push(6);
console.log(arr);  // 输出[1, 2, 3, 4, 5, 6]

// 删除元素
arr.pop();
console.log(arr);  // 输出[1, 2, 3, 4, 5]

// 遍历数组
arr.forEach(function (item) {
  console.log(item);
});
  1. RegExp object
    RegExp object is used to process regular expressions. It provides methods for matching and replacing strings, enabling complex string processing operations.

Sample code:

// 正则表达式匹配
var str = "Hello World";
var pattern = /W/;
var result = pattern.test(str);
console.log(result);  // 输出true

// 正则表达式替换
var newStr = str.replace(pattern, "J");
console.log(newStr);  // 输出Hello Jorld

The above are simple examples of some commonly used JS built-in objects. By understanding and learning these built-in objects, you can better handle operations such as math, dates, strings, arrays, and regular expressions in JS. Of course, the built-in objects of JS have more functions and usages. It is recommended that developers study and practice in depth to enrich their programming skills.

The above is the detailed content of Master common 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