Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Some Weird Facts About JavaScript

DZone's Guide to

Some Weird Facts About JavaScript

Sometimes, JavaScript gets a bad rap as being hard to learn. Well, in this article, we don't really help to dispell this notion. Take a look at some of the quirks of JS!

· Web Dev Zone ·
Free Resource

Bugsnag monitors application stability, so you can make data-driven decisions on whether you should be building new features, or fixing bugs. Learn more.

Arguments Object

The arguments object is an Array-like object corresponding to the arguments passed to a function.

function argumentFunction(){
 console.log(arguments[0]); 
 console.log(arguments[1]); 
 console.log(arguments[2]); 
}
argumentFunction("1","2","3");
Output: 
1
2
3


function Add() {
  var sum = 0;
  for(var j = 0; j < arguments.length; j++) {
    sum += arguments[j];
  }
  return sum;
}
Add(1, 2, 3, 4); // returns 10

Note: It is not an array, however, it is similar to an array and it has a length property in it.

Importance of { } 

function method1() {
   return
   {
      name: 'foo'
   }
}
function method2() {
   return {
      name: 'foo'
   }
}
typeof method1() === typeof method2()   // return false

The first function returns undefined and the second method will return the object as expected. Parentheses play a major role, so they should be handled with caution.

Note: it's good practice to put the opening parentheses on the same line you're working on.

Floating Number :

0.1+0.2  //0.30000000000000004
0.1+0.3  //0.4
0.2+0.4  //0.6000000000000001
0.2+0.5  //0.7
0.3+0.6  //0.8999999999999999
0.3+0.7  //1

Try 0.1+0.2 in your JavaScript environment and it will not return 0.3, however, 0.1+0.3 will return 0.4.

0.1+0.2 == 0.3 will return false, so we should be careful.

Sum of Arrays and Objects :

{} + [] // 0
[] + {} // "[object Object]"
[] + [] // ""
{} + {} // "[object Object][object Object]"

From the above code, we can come to a conclusion that: 

{} + []  == [] + {} // returns false.

These are some of the weird facts about JavaScript. However, there are many and if you find anything weird, please share it in the comment section.

Next Article:  http://www.nodesimplified.com/2017/08/javascript-pass-by-value-and-pass-by-reference.html

If you enjoyed this article, please share it with your developer friends and on social media.


Monitor application stability with Bugsnag to decide if your engineering team should be building new features on your roadmap or fixing bugs to stabilize your application.Try it free.

Topics:
javascript ,web dev ,arguments ,arrays ,objects

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}