Wednesday, June 18, 2014

JavaScript skills



Expression

Value
Why ?
(Hint)
1
"xyz" + false
=
"xyzfalse"
The addition operator produces the sum of numeric operands or string concatenation
String + Boolean
2
33 + true
=
34
Arithmetic operators take numerical values (either literals or variables) as their operands and return a single numerical value
Number + Boolean
3
"foo" + 1
=
"foo1"
The addition operator produces the sum of numeric operands or string concatenation
String + Number
4
"foo" * 1
=
NaN
“foo” not a number to multiply
String * Number
5
function test () {
    var outer = "Hello",
           test = "Bangalore";
    if (outer) {
        var test = " India!";
    }
    alert(outer + test);
}
test();

Hello India!

outer is true. It becomes  Intermediate  scope variable.
Intermediate: No such thing as block scope in JavaScript
Variable scope in JS

1.       any value that is not undefined, null, 0, NaN, or the empty string (""), and any object which has value false  evaluates to true when passed to a conditional statement .

function test ()
{ var outer = 0, test = "Bangalore";
if (outer) { var test = " India!"; }
alert(outer + test);
}
test();



Now output is “0Bangalore” . I have assigned 0 (which is falsy) to outer variable.



2      This is because JavaScript has function-level scope. This is radically different from the C family. Blocks, such as if statements, do not create a new scope. Only functions create a new scope. If there is no block level scope then the variable is accessible thought the functions


In simple statement, the “if” condition in Javascript looks for falsy values. Anything that is not falsy will pass the if condition. That’s easier way to remember this rule. i.e:
if(!falsy) {
    ------- pass,
}
else {
    ------ fail
}

It does not care if the expression inside if evaluates to true or not, cares that it must not evaluate to false or one of the falsy values. As you mentioned below, examples of falsy values are
undefined, null, etc.

To your 2nd response, there is much more to that rule. You don’t need to get into low level details but just in case you feeling curious, there’s something called JS compiler phases in which Javascript interpreter engine parses JS file in multiple loops. In the first phase, it does something called “Variable hoisting” which leads to this behavior (no block level scoping in JS). Refer this if you are interested (might need to read this post few times to make sense out of it J  http://www.adequatelygood.com/JavaScript-Scoping-and-Hoisting.html)


Here is what’s going on in Q5.
-          if(String) evaluates to true
o   Reason:  In JS, any value that is non-falsey is considered true. Examples of falsey values are 0, ‘ ’, undefined, null, {}, etc. Reference
-          a new variable var test= “India”  could modify and retain the value outside its scope/block. Compare this to Java, the output would have been “Hello Bangalore”.
o   Reasons:
§  There is no concept of block level scope in JavaScript. Reference
§  Any variable declared anywhere inside a function is hoisted to the top level. Reference

Effectively Browser interprets the function like this:
function test () {
   var outer;
   var test;

   outer = “Hello”;
   test = “Bangalore”;

    if (outer) {
          test = " India!";
    }
    alert(outer + test);
}

test();

No comments:

Post a Comment