Javascript embarrassment

Nowadays, we all write javascript code using various javascript frameworks, be it Jquery, YUI, prototype or many others. One reason we use this javascript frameworks is that they provide us rich features, easy understanding code, automatic browser compatibility and sometime, langauge extended features. Some of us may even didn’t right much the stand alone javascript code at all, actually, I usually heard people use javascript and Jquery as interchangeable terms. However, know javascript itself will help us understand the underline essence of these frameworks. As I went a quickly refresh some of its basics recently, I realized that, how lucky we have all these wonderful frameworks at our disposal and no need to struggle through those js language blunders result by design. Nonetheless, I share these four javascript quirks from a techpub tutorial; may it will help someone stumble upon this post or just serve as a learning document.

1.Globalization

var myVariable = “title”; // => Document.myVariable

 

2. Coercion

Value 0 == fales // true

“0” == false // true

“0” == “” // false

“0” == false // true

“” == false // true

“” == “0” // false

false == “false” // false

false == undefined // false

false == null // false

null == undefined // true

typeof(null) => object

typeof(undefined) => undefined

False: false, “”, 0, null, undefined

 

3. Coercive Math

true + true = 2

true => 1
false => 0

true – true = 0

true + true == true // false

true + true == false //false

true – true == false // true

true * 5 == false //false

false * 5 == false //true

true * 5 == true // false

 

4. Hoisting

var message = “Hello!”;

function sayHi(){

if(!message){

var message = “There is nothing to say, really…”;

console.log(message);

}

}

sayHi();

 

======>

var message = “Hello!”;

function sayHi(){

var message = undefined;

if(!message){ message = “There is nothing to say, really…”; console.log(message); }

}

sayHi();

No Comments - Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

*