Hidden Features of JavaScript?

0 votes
660 views
asked Nov 8, 2013 in java script by jonathan (3,105 points)
HI

   Hidden Features of JavaScript?

4 Answers

0 votes
answered Nov 8, 2013 by freestyle

Hi jonathan

  Trick 1

          You don't need to define any parameters for a function.

function sum() {
    var retval = 0;
    for (var i = 0, len = arguments.length; i < len; ++i) {
        retval += arguments[i];
    }
    return retval;
}

sum(1, 2, 3) // returns 6

 

0 votes
answered Nov 8, 2013 by freestyle
hi jonathan
  Trick 2
    alert('' == '0'); //false
    alert(0 == ''); // true
    alert(0 =='0'); // true

    == is not transitive. If you use === it would give false for all of these statements as expected.

0 votes
answered Nov 8, 2013 by codetiger
 

You can use the in operator to check if a key exists in an object:

var x = 1;
var y = 3;
var list = {0:0, 1:0, 2:0};
x in list; //true
y in list; //false
1 in list; //true
y in {3:0, 4:0, 5:0}; //true

 

0 votes
answered Nov 8, 2013 by codetiger

You can use the in operator to check if a key exists in an object:

 

var x = 1;
var y = 3;
var list = {0:0, 1:0, 2:0};
x in list; //true
y in list; //false
1 in list; //true
y in {3:0, 4:0, 5:0}; //true

 

...