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

How to convert Javascript operators and data types

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "how to convert Javascript operators and data types". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to convert Javascript operators and data types.

one. If else statement

If else is used to express if in Chinese. Oh, just... , otherwise.

/ / if the condition is true, execute the parenthesis code after if, otherwise execute the parenthesis code after else

If (condition) {

/ / todo

} else {

/ / todo

}

/ / multiple judgment

If () {

} else if () {

} else {

}

Specific examples

Var a = 100

Var b = 20

Debugger

If (a > b) {

Console.log ('a greater than b')

} else {

Console.log ('a less than b')

}

Var gender = prompt ('Please enter gender')

If (gender = = 'male') {

Alert ('you are a man')

} else if (gender = = 'female') {

Alert ('you are a girl')

} else {

Alert ('gender unknown')

}

two. Type conversion

[Tencent document] 05-Type conversion-lesson preparation

Question: what will the following code print?

Var a = 'aa'

Var b = 'bb'

If (aquib) {

Console.log ('xxxxxxx')

} else {

Console.log ('yyyyyyyy')

}

1. Explicit conversion

1.Number (variable name) converts other types to numeric / / shortcut variable names * 1

2.String (variable name); transfer string / / variable name +''

3.Boolean (variable name); / /! Variable name

Var str = '123'

/ / convert to a number

Var num = Number (str)

Console.log (num)

Console.log (typeof num)

/ / the result is NaN

Var str2 = 'abcd'

Var num2 = Number (str2)

Console.log (num2)

/ / the result is 0

Var str3 =''

Var num3 = Number (str3)

Console.log (num3)

two。 Implicit conversion

Var a = 'aa'

Var b = 'bb'

/ / astatb = > aabb aabb is automatically converted to true, so the execution result is xxxxxx

If (aquib) {

Console.log ('xxxxxxx')

} else {

Console.log ('yyyyyyyy')

}

3. Data type conversion rules

[Tencent documents] Type conversion rules Tencent documents

three. Operator

[Tencent document] js operator mind map Tencent document

1. Arithmetic operation

1. Music music /

two。 In an additive life, if a number is a string, then + represents string concatenation

Var a = 20

Var b = 7

Var num1 = aqb

Var num2 = a Murb

Var num3 = aqb

Var num4 = a _ b

/ / calculate the remainder

Var num5 = a% b

Console.log (num1)

Console.log (num2)

Console.log (num3)

Console.log (num4)

Console.log (num5)

/ / if a number is a string, + indicates string concatenation

Var a = 100

Var b = prompt ('Please enter a number')

Console.log (typeof b)

Console.log (a+Number (b))

two。 Relational operator (comparison operator)

Var a = 10

Var b = '10'

Console.log (a = = b); / / true. If the type is different, convert to the same type first.

Console.log (a = b); / / false, different types are false, and then determine whether the values are the same.

3. Logical operator

1. Logic is not! , just take the opposite.

Var boo1 = false; / /! boo1 = > true! bool

Var num = 100; / /! num = > false

Var str =''; / /! str = > true

Var obj = null; / /! obj = > true

Var username; / /! username = > true

two。 Logic and the meaning of & &: in Chinese

Rule: the former is true, the latter is the latter, otherwise the former is the former

When both are Boolean, the result is true only if both are true, otherwise false.

/ / calculate the value of the following formula (Boolean type is all involved in the operation)

True & & true = > true

True & & false = > false

False & & true = > false

False & & false = > false

/ / calculate the value of the following formula (other types participate in the operation)

Var num1 = 100

Var num2 = 0

Num1 & & num2 = > 0

Num2 & & num1 = > 0

Var a = 'web'

Var b

A & & c = > / / an error is reported, the result of the calculation is c, and c is not declared

B & & c = > undefined

3. Logic or | |: in or meaning in Chinese

Rule: the former is true, otherwise the latter

When it is all Boolean life, as long as one is true, the result is true, otherwise it is false.

Console.log (true | | true); / / true

Console.log (true | | false); / / true

Console.log (false | | true); / / true

Console.log (false | | false); / / false

Var num1 = 100

Var num2 = 0

Console.log (num1 | | num2); / / 100

Console.log (num2 | | num1); / / 100

Var a = 'web'

Var b

Console.log (a | | c); / / web

Console.log (b | | c); / / an error is reported. The result of the calculation is c, but c is not declared.

4. Unary calculator

I use the original value first, and then add 1 to it. If there is any other value, I will add 1 to it.

+ + I add 1 to myself first, and then do the operation.

Var I = 10

Var j = 20

Var i2 = 10 + iComplete; / / I do the operation first and then add 1

Console.log ('i2 recording journal i2); / / 20

Console.log ('iMagna I); / / 11

Var J2 = 10 + j; / j add 1 before operation

Console.log ('j2century journal j2); / / 31

Console.log ('jackpot j); / / 21

Self-subtraction operation, which is similar to self-addition operation.

Var I = 10

Var j = 20

Var i2 = 10 + iMurmuri; / / the first operation is to subtract 1.

Console.log ('i2 recording journal i2); / / 20

Console.log (I); / / 9

Var J2 = 10 +-- j; / j subtract 1 before operation

Console.log ('j2century journal j2); / / 29

Console.log ('jackpottery j); / 19

Exercise:

Var I = 10

Var i2 = 10 + iTunes; / / iTunes 10 i2 / 20

Var i3 = 10 + I; / / i4 12 i3 + 22

Var i4 = 10 + iMurmuri; / / iCu11 i4q22

Var i5 = 10 +-- I; / / I = 10 i520

/ / find the value of iQuery i2, i3, 4, and i5

Console.log (iQuery i2, i3, 4, 5)

Var count = 60; / / state the value printed by the console

Console.log (--count); / / 59

Console.log (--count); / / 58

Console.log (- count); / / 57

Console.log (--count); / / 56

Console.log (--count); / / 55

Var I = 1

Var j = 1

Var a = 10

/ / 11 10 + 1

Var b = a + iTunes +

/ / 11 + 3 + 3 + 2

Var c = b + + + iI + iBy + + + j

Var d = a + b + c

/ / iTunes 4 jungles 2 axioms 10 minutes 11, clocks 19 dudes 40

Console.log (iMagnejPerry aPercience bPhonec pr r d)

5. Compound assignment operator

Var a = 10

A + = 10

Console.log (a); / / 20

A-= 10

Console.log (a); / / 10

A * = 10

Console.log (a) / 100

A / = 10

Console.log (a); / / 10

A% = 10

Console.log (a); / / 0

6. Operator precedence

() > self-addition (self-subtraction) > arithmetic > relation > Logic > assignment

At this point, I believe you have a deeper understanding of "how to convert Javascript operators and data types". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Development

Wechat

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

12
Report