Implementation of ECMAScript standard
ES6 = ES2015, ES7 = ES2016 etc.
Up Next: Javascript Review
devdocs.io
, enable:console
Promise
url.parse
methodalert
functionGoogle Is Your Friend
Try It And See
log
error
var me = {
first : 'Sequoia',
last : 'McDowell',
age : 31
};
console.log(me.age); // 31
var me = new Person('Sequoia', 31);
console.log(me.age); // 31
var me = ['Sequoia', 'McDowell', 31];
console.log(me[2]); // 31
"Dynamic" "Duck"
typeof
instanceof
function add(x, y){
return y + x;
}
add(1,2); // => 3
function add(x, y){ return y + x; }
add(3,2); // => 5
Create divide & multiply functions:
// javascript-review/start/mul-and-div.js
console.log(
div(
mul( 21, 40 ),
mul(10, 2)
)
);
//output: 42
//same thing on one line:
console.log( div( mul(21, 40), mul(10, 2) ) );
node filename.js
What's meant by "expression"?
"any valid unit of code that resolves to a value." - MDN
(1 + 1) //expression
("hello world") //expression
(function(){ return 21; }) //expression
(1 + 1) //=> 2
("hello world") //=> "hello world"
(function(){ return 21; }) //=> function(){ return 21 }
typeof (1 + 1) //=> "number"
typeof ("hello world") //=> "string"
typeof (function(){ return 21; }) //=> "function"
var sum = 1 + 1;
var msg = "hello world";
var fun = function(){ return 21; };
var add = function(x, y){
return y + x;
};
add(1,2); // Uncaught TypeError: add is not a function
var add = function(x, y){ return y + x; };
add(3,2); // => 5
var add = function (x, y){
return y + x;
};
var add = function add(x, y){
return y + x;
};
Why use named expressions?
Uncaught TypeError: Cannot read property 'name' of undefined
getUser @ foo.js:2
(anonymous function) @ bar.js:71
(anonymous function) @ bar.js:202
(anonymous function) @ baz.js:11
...
Uncaught Error:
getUser @ foo.js:2
getItem @ bar.js:71
buildDBQuery @ bar.js:202
createDatabase @ baz.js:11
...
Find where I broke the "Always use named expressions" rule and rewrite it as a named expression
javascript-review/start/function-runner.js
Create a function that takes a function argument and...
Hints:
myfunction.name
Extra Credit:
run
returns a function that does 1 & 2 above var wrappedHello = run(helloworld)
wrappedHello();
Up Next: Handling Functions
Functions can be passed around like other values
//filename: javascript-review/set_timeout_example.js
//setTimeout( FUNCTION , NUMBER );
var sayHi = function logHello(){
console.log('hello');
};
var delay = 500; //ms
setTimeout(sayHi, delay);
Up Next: Scope & Hoisting
"The current context of execution." - MDN
Variables & functions stay in the scope in which they are declared
var x;
var y;
function three(){
return 3;
}
x
?var x = 99; // declared in outer scope
function addTen(y){
var x = 10;
return y + x;
}
console.log(addTen(7)); // ??
console.log(x); // ??
A scope has access to its parent scopes
var x = 99; // declared in outer scope
function addTen(y){
x = 10;
return y + x;
}
console.log(addTen(7)); // ??
console.log(x); // ??
Always use var
to declare vars when creating them!!
function myDictionary(){
$ = 'dollars';
_ = 'no dollars';
angular = 'the opposite of round';
}
congrats-server.js
:
Create a server that...
Hints:
javascript-review/start/
Declarations are moved to the top of their scope
foo();
function foo(){
return 10;
}
Interpreter sees:
function foo(){
return 10;
}
foo();
Assignments are not hoisted
console.log(x);
var x = 100;
Interpreter sees:
var x;
console.log(x);
x = 100;
var x = 123;
console.log(x);
console.log(y);
console.log(z);
var y = 456;
z = 789;
add(1,2); // => 3
function add(x, y){ return y + x; }
add(3,2); // => 5
add(1,2); // Uncaught TypeError: add is not a function
var add = function(x, y){ return y + x; };
add(3,2); // => 5
Up Next: Asynchronous Programming