Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the types of JavaScript variables and the conversion methods between variables

Shulou Source: shulou.com Published: 2022-06-01 15:10:05 10月02日 Update

This article mainly introduces the "JavaScript variable types and conversion methods between variables" related knowledge, editor through the actual case to show you the operation process, the method of operation is simple and fast, practical, I hope that this "JavaScript variable types and conversion methods between variables" article can help you solve the problem.

1. Variable type var num = 10; console.log (typeof num)

Typeof: declare variable types

Alert: pop-up alert box

Prompt: input and output statements

1.1 Color of variable type

Identify variable types according to the color of the console:

Purple: numerical type

Black: character type

Blue: Boolean (true false)

Light gray: undefined null

two。 Literal quantity

A literal is a representation of a fixed value in the source code, that is, a literal representation of how the value is expressed.

3. Data type conversion 3.1. Convert to string type 1. Plus sign concatenation string

Console.log (num+' I am the string')

2.toString () converted to string var num=10;var str=num.toString (); console.log (str); 3.string (variable) 3.2 converted to numeric (key) 1.parseInt (string) function-"integer numeric var age=prompt ('Please enter your age'); console.log (parseInt (age)); console.log (parseInt ('3.14')) / / rounding 3 console.log (parseInt ('3.98')); / / rounding 3 console.log (parseInt (' 120px')); / / rounding 1202.parseFloat (string) function-- "floating-point numeric console.log (parseFloat ('3.14'); console.log (parseFloat (' 120px')); / / 120 removes the px unit 3. Using number (variable) var str='123';console.log (number (str); console.log (number ('123')); 4. Take advantage of arithmetic operations-+ * / implicit conversion console.log ('123mur0); / 12console.log (' 123humor 120'); / 3console.log ('123conversation 1); / 1233.3 convert to Boolean type

Boolean () function

Return value: true or false

False: 0, NaN, null, undefined,''(empty)

Console.log (boolean (')); / / falseconsole.log (boolean (0)); / / falseconsole.log (boolean (NaN)); / / falseconsole.log (boolean (null)); / / falseconsole.log (boolean (undefined)); / / false

The rest are true.

This is the end of the content about "what are the types of JavaScript variables and the conversion methods between variables". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Tags: Variable type character method string function literal knowledge Boolean numerical value industry color input different character type practical number content plus sign unit Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Shulou Tech Info macOS Apple Microsoft