echo `whoami`

Ahsan Ayaz

Software Architect

JavaScript

You are

Disclaimer

I

JavaScript

How this session works

  1. I introduce you to something

  2. I ask you a question

  3. You give your best guess

Let the fun begin

typeof

typeof 1    //=> number
typeof 'a'  //=> string
typeof {}   //=> "object"
typeof []   //=> ???
typeof []   //=> "object"

null vs undefined

undefined

var foo; // value is undefined

undefined is the default value assigned to a variable that hasn't been assigned one

null

var person = {
    name: 'Ahsan Ayaz',
    opinionOnJustinBieber: null,
}

person.opinionOnJustinBieber //=> null

 

null is used to explicitly say "there is no value here"

Question

typeof null //=> ???
typeof undefined //=> ???

 

So what would be output of these?

typeof null //=> "object"
typeof undefined //=> "undefined"

 

NaN

Stands for 'Not a number'. Used in math operations when result is invalid

  

Math.sqrt(-1) //=> NaN
parseInt('a') //=> NaN

Questions

typeof NaN //=> ???
NaN==NaN //=> ???
typeof NaN //=> "number"
NaN==NaN //=> false

Takeaways for type checking

// undefined
typeof foo == 'undefined';

// NaN
isNaN(foo);

// null 
foo === null;

scope

global scope

var hello = 2;

function hi(){
    console.log(hello); //=> 2
}

local scope

function callMe(){
    var hi = 8;
}
callMe();
console.log(hi); //=> undefined

Example 1

function foo(){
    if(true){
        var x = 100;
    }
    console.log(x) //???
}

foo();
function foo(){
    if(true){
        var x = 100;
    }
    console.log(x) //100
}

foo();

Example 2

function funky(val){
    val = null;
}
var x = 10;
funky(x);

console.log(x); //???
function funky(val){
    val = null;
}
var x = 10;
funky(x);

console.log(x); //10

Take away for scope

JavaScript is function scoped, not block-scoped

equality

You think you know javascript? LOL

1 === 1
//=> true. same value.

  1 === '1'
//=> false. different types.

   var a = []
   var b = []
   a === b
//=> false, only true if is same object

   a === a
//=> true, is same object

Strict equality (===)

1 == 1
//=> true

loose equality (==)

1 == '1' 
//=> true. the string gets converted into a number.
var a = []
var b = []
a == b

loose equality (==)

// => false
undefined == null // rules of JavaScript say these are equal
null == undefined
=> true

undefined == true // expected, undefined isn't a true value
=> false

undefined == false // wait, but why?
=> false

dem falsy values

  • null and undefined have their own rules about truth and falsity
  • when comparing values of different types, values get converted to the same type, according to some rules (see further reading)
  • just use strict equality already, geez

Take aways from equality

Q/A

Credits

This talk was highly inspired by the following amazing pieces: 

Thank You !

Software Architect

Modus Create

JavaScript - You are weird

By Ahsan Ayaz

JavaScript - You are weird

A talk happened at TechKaro - Karachi, Pakistan to students and freshers learning JavaScript.

  • 151
Loading comments...

More from Ahsan Ayaz