JS
JS
console.log(“Hello World”);
Vedant Seta
JS Everything
● Client Side
● Server Side
● Desktop/Mobile Apps
● Task Runners
Challenge #1
a = 2;
var a;
console.log( a );
setTimeout(function(){
console.log('When will this run?');
}, 0);
console.log('Here');
Challenge #3
function test(a) {
console.log(a, b);
}
b=2;
test(1);
● If a variable isn’t found in current scope, it will look at parent till global
scope is reached.
JS is
● Single Threaded
● Non Blocking APIs
● Asynchronous
● Call Stack
● Web APIs
● Callback queue
● Event Loop
Hoisting
● expression
● statement
● method
● Constructor
● closures
Function Expression
function fnName (parameters) {
// statements
return; or return expression;
}
// Call
fnName(params);
Question : Functions
function test() {
Var a = 10;
return
a;
}
console.log(test());
Function Statement
function fnName (parameters) {
var fnName = function(){
// statements
};
}
Expands to
var fnName = undefined;
// Call
fnName = function fnName() {
fnName(params);
}
Challenge
alert(foo()); alert(foo());
function foo(){
function foo(){ return bar();
function bar() { var bar = function() {
return 3; return 3;
} };
return bar(); var bar = function() {
function bar() { return 8;
return 8; };
} }
}
Functions as Object
function Employee(name) {
this.name = name;
}
var emp1 = new Employee(‘vedant’);
Closure
● Scope which inner function enjoys even after the parent function has
returned
● Permanent link between function and its scope
● Holds scope chain preventing garbage collection
Question : Closures
var a = {};
for(var i = 0; i < 3; i++) {
a[i] = function(){
console.log(i);
}
}
a[0]();
a[1]();
a[2]();
Functions call / invocation
● Not itself
● Not author time binding
● Not how it is declared
● Let’s DEBUG
Object Returns
honda car
getName = function
Inheritance
● Map
● Reduce
● Filter
Object Functions
● Assign
● Keys
● Values
● Freeze
● Create
JSON