node sample.js
var x = 10
function x() {
return 10;
}
NaN
1
1.01
Infinity
-0
0xff
0o777
0b110101010101011101001010
true
false
''
""
'a'
' '
'1'
var emptiness = null;
undefined;
var emptiness;
// truthy
var x = 'a';
if(x) {
console.log(x);
}
// falsey
var x;
if(x) {
console.log(x);
}
var person = {
firstName: 'Jacky',
lastName: 'Chan'
}
// accessing using dot notation
console.log(person.firstName);
// accessing using strings
console.log(person['firstName']);
// accessing using variable name
var key = 'firstName';
console.log(person[key]);
// assigning using a variable
key = 'middleName';
person[key] = 'Jay'
console.log(person);
// assigning using dot notation
person.lastName = 'Dan'
console.log(person);
function replace(ref) {
ref = {}; // this code does not affect the object passed
}
function update(ref) {
ref.key = 'newvalue'; // this code affects the contents of the object
}
var a = { key: 'value' };
replace(a); // a still has its original value - it's unmodfied
console.log('after replace:', a)
update(a); // the contents of 'a' are changed
console.log('after update:', a);
function first() {
var a = "hello first"
second();
function second() {
// var a = "hello second"
third();
function third() {
console.log('third', a); // prints "third hello first"
fourth();
function fourth() {
// do something
console.log('fourth', a); // prints "fourth hello first"
console.log(b); // prints "hello global"
}
}
}
}
var b = "hello global";
first();
function fullName() {
return this.firstName + ' ' + this.lastName;
}
var person = {
firstName: 'Don',
lastName: 'Burks',
fullName: fullName
}
console.log(person.fullName());