fes Logo

Javacript

Reserved Words

Numbers

  • Positive / Negative
  • NaN is a number but is not avalue - isNaN()

Strings

  • string literal can be between '' or ""
  • escape character \
  • Print to console - ''Hello World''
  • Tab = \t
  • Carriage Return = \r
  • New line = \n

Strings cont...

  • length property
  • Concatenation

Booleans

  • true / false

Empty Types

  • null
  • undefined

Variables

  • var name = 1; // int / number
  • var name2 = 'hi!'; // string

typeof

  • what am i?

Type Conversion

console.log(8 * null)
// → 0
console.log("5" - 1)
// → 4
console.log("5" + 1)
// → 51
console.log("five" * 2)
// → NaN
console.log(false == 0)
// → true

Operators

  • operators for arithmetic (+, -, *, /, and %)
  • string concatenation (+)
  • comparison (==, !=, ===, !==, <, >, <=, >=)
  • logic (&&, ||)

Unary Operators

  • - to negate a number
  • ! to negate logically
  • typeof to find a value’s type

Ternary Operator

  • ? :

Conditionals

Controlling the flow of execution

If / Else

if (true){ } else { }

Else if

if (true){ } else if { } else {}

While

while () { }

Do While

do{} while()

Execute body once

For loop

for(var i =0; i < 100; i++) { }

break

for(var i =0; i < 100; i++) { break; }

counter shortcuts

  • ++ / --
  • += / -=

switch

switch(){ case "red": break; default: break; }

Functions

DRY - Dont repeat yourself

Creating Functions

function name() {} //moved to top

var name = function() {}

Scopes

global

local

Nested Scope

  • inner can see outer
  • outer cannot see inner

Arrow functions

  • name = x => { do stuff }
  • shorthand

Questions?