`
android_madness
  • 浏览: 40896 次
  • 性别: Icon_minigender_1
  • 来自: 福州
社区版块
存档分类
最新评论

Why does [1,2] + [3,4] = “1,23,4” in JavaScript?

阅读更多
I wanted to add the elements of an array into another, so I tried this simple sentence in our beloved Firebug:
[1,2] + [3,4]
It responded with:

"1,23,4"
What is going on?

-----------------------------------------------------


JavaScript has 6 built-in data types:

undefined
boolean
number
string
function
object

Note that neither null nor [] is a separate type - both return object when fed to typeof. However + works differently in either case.

That's right - JavaScript has no primitive arrays as such; only instances of a class called Array with some syntactic sugar to ease the pain.

Adding more to the confusion, wrapper entities such as new Number(5), new Boolean(true) and new String("abc") are all of object type, not numbers, booleans or strings as one might expect. Nevertheless for arithmetic operators Number and Boolean behave as numbers.

Easy, huh? With all that out of the way, we can move on to the overview itself.

Different result types of + by operand types:
-------------------------------------------------------------------------------------------
            | undefined | boolean | number | string | function | object | null   | array  |
-------------------------------------------------------------------------------------------

undefined   | number    | number  | number | string | string   | string | number | string |

boolean     | number    | number  | number | string | string   | string | number | string |

number      | number    | number  | number | string | string   | string | number | string |

string      | string    | string  | string | string | string   | string | string | string |

function    | string    | string  | string | string | string   | string | string | string |

object      | string    | string  | string | string | string   | string | string | string |

null        | number    | number  | number | string | string   | string | number | string |

array       | string    | string  | string | string | string   | string | string | string |

-------------------------------------------------------------------------------------------
* this applies to Chrome 13, Firefox 6, Opera 11 and IE9. Checking other browsers and versions is left as an exercise for the reader.

Note: As pointed out by CMS, for certain cases of objects such as Number, Boolean and custom ones the + operator doesn't necessarily produce a string result. It can vary depending on the implementation of object to primitive conversion. For example var o = { valueOf:function () { return 4; } }; evaluating o + 2; produces 6, a number, evaluating o + '2' produces '42', a string.
分享到:
评论

相关推荐

    Technical 5 in 1 Box Set [Chromecast Linux XML PHP Python]

    Technical 5 in 1 Box Set: Book 1: Chromecast + Book 2: Linux for Beginners + Book 3: XML Programming + Book 4: PHP Programming + Book 5: Python Programming by Jack Brandson English | 30 Jul 2016 | ...

    oauth2.in.action

    We want you to come away from this book with a deep understanding of what OAuth can do, why it works the way that it does, and how to deploy it properly and securely in an unsafe internet. ...

    c# ASP.MVC websocket在线聊天

    2. **WebSocket连接管理**:服务器需要维护一个WebSocket连接池,记录每个在线用户的连接状态。当用户打开聊天页面时,通过JavaScript创建WebSocket连接,并将连接标识与用户ID关联。 3. **消息模型**:定义聊天...

    BobBuilder_app

    I deferred from testing the get test over 100 million record as it would require a huge array in memory to store the Guid keys for finding later, that is why there is a NT (not tested) in the table. ...

    flash标签云 3D效果 PHP插件 by weefselkweekje

    1. Add the following code anywhere in your theme to display the cloud. `<?php wp_cumulus_insert(); ?>` This can be used to add WP Cumulus to your sidebar, although it may not actually be wide enough ...

    EO.WebBrowser_for_.NET_2018.2.53.0

    What if user disables JavaScript in Internet Explorer's settings dialog? These questions does not exist with EO.WebBrowser because everything is embedded inside our DLL files. • Native .NET ...

    php.ini-development

    Please see the actual settings later in the document for more details as to why ; we recommend these changes in PHP's behavior. ; display_errors ; Default Value: On ; Development Value: On ; ...

    The.Dart.Programming.Language.03219277

    based, object-oriented language that simplifies the development of structured modern apps, scales from small scripts to large applications, and can be compiled to JavaScript for use in any modern ...

    CocoStudio(cocos2dx编辑器,集合动作,UI,等)

    A programmer can then write the code in Javascript to bring the game alive. CocoStudio的安装 1.CocoStudio的运行平台是Windows操作系统,推荐使用Windows7操作系统。 2.安装CocoStudio之前,确保电脑中安装了...

    GitHub1s_chrome_extension:GitHub1s chrome 扩展

    Why does my chrome extension ask for history permissions? This is the standard warning for the "tabs" permission. It allows you to query, and be notified of changes, to URLs of all tabs. This allows ...

    UE(官方下载)

    For example, If ^(h*o^) ^(f*s^) matches "hello folks", ^2 ^1 would replace it with "folks hello". Perl compatible regular expressions An introduction to using Perl-style regular expressions for ...

    how-webpack-works.pdf

    At its core Webpack allows us to use javascript modules within our browser by taking multiple files and assets and combining them into one big file as shown below in this image from the new docs for ...

    The Dart Programming Language

    Throughout, he covers both language semantics and the rationale for key features, helping you understand not just what Dart does, but why it works the way it does. You will learn about Dart’s object...

Global site tag (gtag.js) - Google Analytics