Skip to content

Instantly share code, notes, and snippets.

@kowsheek
Last active October 4, 2017 13:08
Show Gist options
  • Save kowsheek/b1c6583b00cdd295f6dc159ee6cf08a2 to your computer and use it in GitHub Desktop.
Save kowsheek/b1c6583b00cdd295f6dc159ee6cf08a2 to your computer and use it in GitHub Desktop.

Recap

Running code

node sample.js 

Variables

var x = 10 

Functions

function x() {
    return 10;
}

Primitives

Numbers

NaN
1
1.01
Infinity
-0
0xff
0o777
0b110101010101011101001010

Boolean

true
false

Strings

''
""
'a'
' '
'1'

Null

var emptiness = null;

Undefined

undefined;
var emptiness;

Truthy/Falsey

// truthy
var x = 'a';
if(x) {
  console.log(x);
}

// falsey
var x;
if(x) {
  console.log(x);
}

Objects

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);

Functions

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);

Scopes & this

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());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment