Home > Web Front-end > Front-end Q&A > A detailed look at type conversions in JavaScript

A detailed look at type conversions in JavaScript

PHPz
Release: 2023-04-23 19:49:34
Original
662 people have browsed it

JavaScript is a very flexible programming language that supports multiple data types. Any type conversion (any type conversion) is one of the problems often encountered in JavaScript.

Data types in JavaScript can be divided into primitive types and object types. Primitive types include numbers, strings, Boolean values, null and undefined, and object types include Object, Function, Array, etc.

Any type of data can be converted into other types of data using type conversion rules. Certain conversion rules need to be followed during this conversion process.

Below we will introduce type conversion in JavaScript in detail.

Explicit type conversion

JavaScript provides some built-in functions that can convert any type of data into other types of data. These functions can be used like ordinary functions, as shown below:

// 将数字转换为字符串
var num = 123;
var str = num.toString(); // str 将会是 "123"

// 将字符串转换为数字
var str = "123";
var num = Number(str); // num 将会是 123

// 将字符串转换为布尔值
var str = "123";
var bool = Boolean(str); // bool 将会是 true
Copy after login

In these functions, toString(), Number(), and Boolean() are used for explicit type conversion.

In JavaScript, you can also use cast operators (such as , -, , --, etc.) to perform explicit type conversion.

When performing explicit type conversion, you need to pay attention to the following points:

  • When numbers and strings are added, JavaScript will automatically convert the number to a string, and then Perform string concatenation. The sample code is as follows:
var num = 123;
var str = "456";
var result = num + str; // result 将会是 "123456"
Copy after login
  • When strings and Boolean values ​​are added, JavaScript converts the Boolean value to a number before adding it. The sample code is as follows:
var str = "123";
var bool = true;
var result = str + bool; // result 将会是 "123true"
Copy after login
  • When a Boolean value is added to a number, JavaScript converts the Boolean value to a number before adding it. The sample code is as follows:
var num = 123;
var bool = true;
var result = num + bool; // result 将会是 124
Copy after login

Implicit type conversion

In JavaScript, some operators and functions implicitly convert one data type to another type.

Implicit type conversion of strings and numbers

When strings and numbers are compared or operated, JavaScript will automatically convert the string into a number and then perform the comparison or operation. The sample code is as follows:

var str = "123";
var num = 456;
var result1 = str + num; // result1 将会是 "123456"
var result2 = num + str; // result2 将会是 "456123"
Copy after login

In the above code, the symbol can be used to splice strings or add numbers. Due to the operation of strings and numbers, JavaScript will automatically convert strings into numbers. So the final result is a string.

Implicit type conversion of Boolean values

In JavaScript, when a Boolean value participates in a comparison or operation, the Boolean value is automatically converted into a numeric type for comparison or operation. True turns to 1, False turns to 0. The sample code is as follows:

var num = 5;
var bool1 = num == true; // bool1 将会是 false
var bool2 = num == false; // bool2 将会是 false
var bool3 = num > false; // bool3 将会是 true
Copy after login

Automatic type conversion

In JavaScript, some operations will automatically trigger type conversion.

In JavaScript, all non-boolean types in if, for, while, etc. statements will automatically trigger type conversion.

Let’s give examples below:

Automatic type conversion in if statements

In JavaScript, non-Boolean types in if statements will automatically trigger type conversion. When a non-Boolean type is used as a condition, JavaScript calls the Boolean() function to convert it to a Boolean value type.

The following example illustrates converting the non-Boolean type in the if statement into a Boolean type:

if (null) {
    console.log("null is true");
} else {
    console.log("null is false");
}

if (undefined) {
    console.log("undefined is true");
} else {
    console.log("undefined is false");
}

if (0) {
    console.log("0 is true");
} else {
    console.log("0 is false");
}

if ("") {
    console.log("'' is true");
} else {
    console.log("'' is false");
}
Copy after login

The output results are as follows:

null is false
undefined is false
0 is false
'' is false
Copy after login

As can be seen from the above output results, When a non-Boolean type is used in an if statement, it needs to be converted to a Boolean type before making a judgment.

Automatic type conversion in for loop

In JavaScript, non-Boolean types in for loops will automatically trigger type conversion. When a non-Boolean type is used as a condition in a for loop, JavaScript calls the Boolean() function to convert it to a Boolean value type.

The following example illustrates converting non-Boolean types in the for loop into Boolean types:

for(var i=0; i<=10; i++) {
    if(i) {
        console.log(i);
    }
}
Copy after login

The output results are as follows:

1
2
3
4
5
6
7
8
9
10
Copy after login

As can be seen from the above output results, In a for loop, non-Boolean types can also be converted to Boolean types and then judged.

Automatic type conversion in while loop

In JavaScript, non-Boolean types in while loops will automatically trigger type conversion. When a non-Boolean type is used as a condition in a while loop, JavaScript calls the Boolean() function to convert it to a Boolean value type.

The following example illustrates converting the non-Boolean type in the while loop into a Boolean type:

var i = 0;
while(i < 10) {
    i++;
    if(i%2) {
        console.log(i + "是奇数");
    } else {
        console.log(i + "是偶数");
    }
}
Copy after login

The output results are as follows:

1是奇数
2是偶数
3是奇数
4是偶数
5是奇数
6是偶数
7是奇数
8是偶数
9是奇数
10是偶数
Copy after login

As can be seen from the above output results, in the while loop In the loop, non-Boolean types can also be converted to Boolean types and then judged.

Summary

In JavaScript, any type conversion needs to follow certain rules. During the type conversion process, you need to pay attention to several key points:

  • Data types in JavaScript include primitive types and object types;
  • Explicit type conversion can use type conversion functions or forced type conversion operators;
  • Implicit Type conversion is a common type conversion method in JavaScript;
  • Automatic type conversion means that non-Boolean types in if, for, while and other statements will automatically trigger type conversion;
  • In When performing any type conversion, you need to pay attention to the rules of type conversion to ensure the correctness of the code.

The above is a detailed introduction to any type conversion in JavaScript. I hope it can provide you with some help.

The above is the detailed content of A detailed look at type conversions in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template