Ten built-in object lists in JavaScript and their programs

JavaScript includes several built-in objects that are standardized by ECMAScript and implemented by browser vendors. These objects encapsulate APIs for handling data and performing operations on it. There are ten core built-in objects in JavaScript, which include: String, Boolean, Number, Array, Date, Math, Error, Function, Object, and Global. Wrapper types such as String, Number, and Boolean wrap primitive data types and provide methods and properties to manipulate them. The reason for using wrapper types is to allow primitive data types to have access to methods and properties, just like reference types. When you call a method or access a property of a primitive value, the JavaScript engine automatically creates a corresponding wrapper object. Once the operation is completed, the wrapper object is discarded.

The String object is used for manipulating text strings. You can create a primitive string variable like this:
var stuName = 'Smith';
var gender = 'male';
var priceString = String(150.5);
To create a reference type string object, use:
var carType = new String('bmw528LI');
The String wrapper provides various methods, such as str.length to get the number of characters, str.toLowerCase() to convert the string to lowercase, and str.toUpperCase() to convert it to uppercase. For example:

var str = "Hello,World";
str = str.toLowerCase();
var str1 = str.toUpperCase();
document.write(str + str1);
// Output: hello, world HELLO, WORLD

Note that the result of these methods must be assigned to a new variable, as strings are immutable in JavaScript. You cannot modify the original string directly; instead, you must create a new one. Always remember to store the returned value from any string API call. This is often tested in interviews. Strings are essentially arrays of characters, and each character can be accessed using charAt(). For instance:

var pid = "371471194710040056";
var char = pid.charAt(pid.length - 2);
console.log(char % 2 == 0 ? "Female" : "Male");

For encoding and decoding, you can use charCodeAt() and fromCharCode(). Here's an example of converting characters to their Unicode values and then back:

var input = prompt("Please enter the string to be converted");
var result = [];
for (var i = 0; i < input.length; i++) {
    result.push(input.charCodeAt(i));
}
document.write("The character you typed: " + input + "---" + "converted character: " + result.join(""));

When dealing with strings, it's important to optimize performance. Instead of using += repeatedly, it's better to push substrings into an array and then join them. This is more efficient than using the toString() method. Common string operations include searching for keywords, replacing them, and extracting substrings. For example, using indexOf() to find the position of a keyword:

var str = "Today is very happy Friday on Friday";
var index = str.indexOf("Friday");
console.log("Location: " + index + " Found keyword");

index = str.indexOf("Friday", index + 1);
console.log("Location: " + index + " Found keyword");

To find all occurrences, you can use a loop:

var str = "Today is very happy Friday on Friday";
var index = -1;
while ((index = str.indexOf("Friday", index + 1)) != -1) {
    console.log("Location: " + index + " Found keyword");
}

The slice() method is useful for extracting parts of a string. It works similarly to substring(), but does not support negative indices. For example:

var str = "345681994412270088";
var subStr = str.slice(5, 8 + 1);
var subStr1 = str.slice(-12 - 1, -9);
document.write("Your birth year is " + subStr);
document.write("Your birth year is " + subStr1);

Pattern matching using regular expressions is another powerful feature. You can define patterns like /no/i and use them to search or replace text:

var str = "No zuo no die";
var reg = /no/ig;
str = str.replace(reg, "yes");
document.write(str);

The match() method allows you to extract matches from a string. It returns an array if multiple matches are found:

var str = "No zuo no die";
var reg = /no/ig;
var kwords = str.match(reg);
if (kwords !== null) {
    document.write(kwords);
    document.write("replaces " + kwords.length);
}

Regular expressions are essential for validating formats, finding patterns, and replacing content. They allow you to define complex rules for matching characters, such as [a-zA-Z] for letters or \d for digits. Understanding how to use these tools effectively can greatly improve your ability to work with strings in JavaScript.

T10 Series Terminal Blocks

Withstand high voltage up to 750V (IEC/EN standard)

UL 94V-2 or UL 94V-0 flame retardant housing

Anti-falling screws

Optional wire protection

1~12 poles, dividable as requested

Maximum wiring capacity of 10 mm2

10 amp Terminal Blocks,high quality terminal connectors,10 mm² terminal blocks,BELEKS T10 series connector strips

Jiangmen Krealux Electrical Appliances Co.,Ltd. , https://www.krealux-online.com