In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you why [1Magne2] + [3mag4] in javascript is not equal to [1Magne2], the content is concise and easy to understand, and can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Someone asked: arrays-Why does [1jue 2] + [3jue 4] = "1m 23pr 4" in JavaScript?
0. problem
I want to append one array to another, so I write the following code in firebug:
[1pr 2] + [3pr 4]
But, unexpectedly, it outputs:
"1, 2, 3, 4."
Without outputting what I expected:
[1,2,3,4]
What's going on? Why is it that [1Jing 2] + [3JI 4] is not equal to [1BZ 2 mei 3je 4]?
1. Solution
The + operator of JavaScript serves two purposes:
Add up the two numbers
Concatenate two strings.
The specification does not define the behavior of the + operator on an array, so javascript first converts the array to a string, and then performs the + operation on the string.
If you want to join two arrays, you can use the array's concat method:
Overview of the + operator in [1,2] .concat ([3,4]) / / [1,2,3,4] javascript
JavaScript has six built-in data types: (translation note: from the given connection, the original author should mean the data type of the original type system, JavaScript actually has two type systems. The first set of type system is identified by typeof, which is called primitive type system, while the second set of type system is based on it and developed from the type of object, that is, object type system, and object type system is identified by instanceof. @ justjavac)
Undefined
Boolean
Number
String
Function
Object
It is important to note that null and [] are two distinct types, and when using the typeof operation, they both return object. But when using the + operator, it works differently in these two cases.
In JavaScript, an array is not a basic type, its existence is just a sugar-coated syntax, it is actually an instance of the Array class. (ps:function is actually the sugar-coated syntax for Function instances.)
If you're still clear-headed right now, it's time to add some news. Javascript's object wrapper types such as new Number (5), new Boolean (true), and new String ("abc") are also object types, which are not numbers, Boolean, strings. However, for the arithmetic operators Number and Boolean, numbers are represented.
Remember the + operator I mentioned earlier? It operates on numbers and strings, that is, Number,Boolean,String or number,boolean,string.
The table below is the type of result obtained by the + operator for different types.
This table applies to Chrome 13, Firefox 6, Opera 11 and IE9.
Note: the + operation of a user-defined object does not always produce a string result. This mainly depends on how the conversion from object type to native type is implemented.
For example:
Var o = {valueOf: function () {return 4;}}
The calculation o + 2 will get 6, which is a numeric number; calculation o +'2' to get '42percent, which is a string string.
The above content is why [1magin2] + [3pyr4] in javascript is not equal to [1mem2pr 3pr 4]. Have you learned knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.