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 use the addition operator of Javascript

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Today, the editor will share with you the relevant knowledge about how to use the addition operator of Javascript. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

Brief introduction

JavaScript is a great language. I like its flexibility: just do things the way you like: change variable types, dynamically add methods or properties to objects, use operators for different variable types, and so on.

However, dynamics comes at a price, and developers need to know how to handle type conversions for different operators: plus sign (+), equal sign (= = and =), unequal sign (! = and! = =), and so on. Many operators have their own ways of handling type conversions.

Addition operator

The most commonly used operator: +, which is used to concatenate strings or sum numbers:

String concatenation:

Var result = "Hello," + "World!"; / / string + string = string (concatenation) / / "Hello, World!"

Arithmetic addition of numbers:

Var result = 10 + 5 ram / number + number = number (sum) / / 15

JavaScript allows you to use objects, arrays, null, or undefined as operands. Let's try to uncover the general rules of conversion.

Conversion rule

Use the following equation to see how JavaScript does the type conversion in the operator:

If at least one Operand is an object, it is converted to the original value (string, number, or Boolean)

After conversion, if at least one Operand is of type string, the second Operand is converted to a string and a concatenation is performed.

In other cases, both operands are converted to numbers and arithmetic addition operations are performed.

If both operands are primitive, the operator checks whether at least one of them is of string type, and if so, performs a concatenation operation. Everything else is converted to numbers and rounded up.

The object type is changed to the original type

Conversion of object type to original type

If the object type is Date, the object's toString () is called.

In other cases, if valueOf () returns a primitive type, the object's valueOf () is called.

In other cases (if valueOf () does not exist or does not return the original type), the toString () method is called, and in most cases this conversion is used.

When an array is converted to a primitive type, JavaScript uses its join (',') method. For example, [1rect 5je 6] is "1rect 5je 6". The primitive type of normal JavaScript object {} is "[object Object]".

Learning examples

The following examples help us understand simple and complex transformation scenarios.

Example 1: numbers and strings

Var result = 1 + "5"; / / "15"

Parsing:

1 + "5" (the second Operand is a string, changing the number 1 to "1" based on rule 2)

"1" + "5" (string concatenation)

"15"

The second Operand is a string, and the first Operand is converted from a number to a string and then concatenated.

Example 2: numbers and arrays

Var result = [1pyrm 3pr 5] + 1; / / "1pr 3pr 51"

Parsing:

] + 1 (using rule 1, convert the array [1mage3re5] to the original value: "1pd3re5")

"1BI 3Jing 5" + 1 (use rules to convert the number 1 to the string "1")

"1pr 3pr 5" + "1" (string concatenation)

"1pr 3pr 51"

The first Operand is an array, so it is converted to the original string value, and in the next step, the numeric Operand is converted to a string, and then the concatenation of the two strings is completed.

Example 3: numbers and Boolean types

Var result = 10 + true; / / 11

Parsing:

10 + true (converts a Boolean true to the number 1 based on rule 3)

10 + 1 (evaluate two numbers)

eleven

Because neither Operand is a string, the Boolean value is converted to a number, and then the sum of the arithmetic is performed.

Example 4: numbers and objects

Var result = 15 + {}; / / "15 [object Object]"

Parsing:

"15 + {}" (the second Operand is an object, applying rule 1 to convert the object to the primitive type string "[object Object]")

15 + "[object Object]" (use rule 2 to convert the number 15 to the string "15")

"15" + "[object Object]" (string concatenation)

"15 [object Object]"

The second object Operand is converted to a string value, because the valueOf () method returns the object itself, which is not the original value, the toString () method is called and returns a string, and the second Operand is now a string, so the number is converted to a string, and finally the concatenation of the two strings is performed.

Example 5: numbers and null

Var result = 8 + null; / / 8

Parsing:

8 + null (because neither Operand is a string, convert null to the number 0 based on rule 3)

8 + 0 (addition of numbers)

eight

Because the Operand is neither an object nor a string, null is converted to a number and then calculates the sum of the number.

Example 6: strings and null

Var result = "queen" + null; / / "queennull"

Parsing:

"queen" + null (because the first Operand is a string, convert null to the string "null" based on rule 2)

"queen" + "null" (string concatenation)

"queennull"

Because the first Operand is a string, the null is converted to a string and then concatenated.

Example 7: numbers and undefined

Var result = 12 + undefined; / / NaN

Parsing:

12 + undefined (convert undefined to numeric NaN based on rule 3 because operands are not objects or strings)

12 + NaN (addition of numbers)

NaN

Because none of the operands are objects or strings, undefined is converted to a number: NaN, and the addition of a number and NaN equals NaN.

These are all the contents of the article "how to use the addition operator of Javascript". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

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