Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Application example Analysis of JavaScript Type conversion

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "JavaScript type conversion application case analysis". In daily operation, I believe many people have doubts about JavaScript type conversion application case analysis. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "JavaScript type conversion application case analysis". Next, please follow the editor to study!

Type of operator

The typeof operator can help you find the type of your variable. The typeof operator returns the type of a variable or expression.

Example:

Project JavaScript typeof operator

The typeof operator returns the type of variable or expression:

[xss_clean] (typeof "" + "

"+ typeof" Json "+"

"+ typeof" 42 "+"

"+ typeof 42 +"

"+ typeof true +"

"+ typeof false +"

"+ typeof undefined +"

"+ typeof null +"

"+ typeof {name:" Json ", age: 22} +"

"+ typeof [2,4,6,8] +"

"+ typeof function myFunc () {})

Note: the typeof operator returns "object" for the array because it is an object in the JavaScript array.

You can use the typeof operator to look up the data type of the JavaScript variable.

Second, type conversion 1. Convert a value to a string

Explicitly convert the value to a string by calling the String () function or the x.toString () method. Through the String () function, the value true is passed to the parameter, thus converting the Boolean value to a string.

String (true); / / returns "true"

The number is passed to the function.

String (108); / / returns "108"

Use the typeof operator to check the type.

Typeof String (true); / / return "string" typeof String; / / return "string"

X.toString () uses the method in a similar manner. You can replace x with a variable.

Let temp = 108; document.querySelector ("# output") [xss_clean] = temp.toString ()

Alternatively, you can put the value in parentheses instead of using x.toString () to set the variable.

(9048) .toString (); / return "9048" (false) .toString (); / / return "false" (50 + 20) .toString (); / / return "70"

By using String () or x.toString (), you can explicitly convert the value of the Boolean or number data type to a string value to ensure that your code works as expected.

two。 Convert a value to a Boolean value

Convert a number or string to a Boolean value, using the Boolean () function. Any value interpreted as null (such as the number 0, an empty string, or an undefined or NaN or null value) is converted to false.

[xss_clean] (Boolean (0) + "

"+ Boolean (") + "

"+ Boolean (undefined) +"

"+ Boolean (NaN) +"

"+ Boolean (null))

Other values are converted to true, including string text consisting of spaces.

[xss_clean] (Boolean (50) + "

"+ Boolean (- 50) +"

+ Boolean (3.14) + "

"+ Boolean (" false ") +"

"+ Boolean (" Hello World "))

Note:

"0" as a string literal is converted to true because it is a non-empty string value.

Boolean ("0"); / / returns true

Converting numbers and strings to Boolean values enables the evaluation of data within the binary and can be used to specify a specified stream in the program.

3. Convert an array to a string

The toString () method converts an array to a string of (comma-separated) array values.

Var months = ["Jan", "Feb", "Mar", "Apr", "May"]; document.getElementById ("result") [xss_clean] = months.toString ()

The join () method also converts all elements of the array to a new string.

This method behaves like toString (), and you can also specify a delimiter.

Var fruits = ["Banana", "Apple", "Mango"]; var result = document.getElementById ("result"); result [XSS _ clean] = fruits.join ("") + "

Result [XSS _ clean] + = fruits.join (") +"

Result [XSS _ clean] + = fruits.join ("+") + "

Result [XSS _ clean] + = fruits.join ("/") + "

Result [XSS _ clean] + = fruits.join ("©")

4. Convert a string to an array

The split () method splits the string into an array of substrings and then returns a new array, using the split () method to separate the array "" with the space characters represented.

Var str = 'Helow World is an Amfhyeaadehe.'; var arr = str.split (")

There is a new array in the arr variable, and you can access each element using an index number.

Arr [0]; / / Air arr [2]; / / is

Use "I" as the delimiter.

Function myFunc () {var str = 'Air Pollution is introduction of chemicals to the atmosphere.'; var arr = str.split ("I"); document.getElementById (' x') [xss_clean] = arr;}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report