JavaScript is WEIRD

I Like JavaScript

Who am I?

Chris on Code

@chrisoncode

scotch.io - digitalocean.com

🙌 Show of hands 🙌

What are we doing?

Unary Operators

  • + 👉 Convert to a number
  • ++ 👉 Increment
  • -- 👉 Decrement
  • ! 👉 Logical not
  • !! 👉 Convert to a boolean
  • typeof 👉 Check the type
  • ~ 👉 Inverts all bits. Returns number
  • delete 👉 Remove from array
  • void 👉 Discards a return value

let partyLevel = 7;

const newLevel = partyLevel++;

newLevel;

7


let partyLevel = 7;

partyLevel++;

partyLevel;

8


let partyLevel = 7;

partyLevel++; 




++partyLevel;

7

8



let partyLevel = '35';

+partyLevel

35




let partyLevel = 'hey im here';

+partyLevel

NaN

typeof hi


typeof null


typeof NaN


typeof /ab+c/


typeof document

undefined

object

number

object

object

typeof Infinity


typeof 'infinity'


typeof (() => 'hello')


typeof [1, 2, 3]


typeof new Array()

number

string

function

object

object

typeof new String()


typeof new Date()


typeof new Number()


typeof new Object()


typeof new Boolean()

!!!object!!!

Hoisting





console.log(typeof myVariable)

undefined



console.log(myVariable)

Uncaught ReferenceError


sayWhatup();


function sayWhatup() {
    alert('whatup')
}

alert




console.log(message);


var message = 'HEY everybody!';

undefined


var message;


console.log(message);


message = 'HEY everybody!';

undefined


sayWhatup();


const sayWhatup = () => {
    alert('whatup')
}

Uncaught ReferenceError:

sayWhatup is not defined

var double;

function double(num) {
  return (num * 2);
}

console.log(typeof double)

function

function double(num) {
  return (num * 2);
}

var double;

console.log(typeof double)

function

function double(num) {
  return (num * 2);
}

let double;

console.log(typeof double)

ERROR: Duplicate declaration "double"

this

var name = 'Scotch';

var person = {
  name: 'Chris',
  printName() {
    console.log(this.name);
  }
}

person.printName();

Chris

var name = 'Scotch';

var person = {
  name: 'Chris on Code',
  printName: () => {
    console.log(this.name);
  }
}

person.printName();

Scotch

const person = {
  name: 'Chris',
  printName() {
    console.log(this.name);
  }
}

const newPrintName = person.printName;


newPrintName();

undefined

const person = {
  name: 'Chris on Code',
  printName() {
    console.log(this.name);
  }
}

const newPrintName = person.printName;


newPrintName.call(person);

Chris on Code

scope

var x = 10;

function fistPump() {
  x = 20;       
}

fistPump();

console.log(x)

20

function doSomethingCool() {
  var x = 5;    

  if (true) {
    var x = 10;     
    console.log(x); 
  }

  console.log(x);
}

doSomethingCool();

10

10

function doSomethingCool() {
  let x = 5;    

  if (true) {
    let x = 10;     
    console.log(x); 
  }

  console.log(x);
}

doSomethingCool();

10

5

Coercion

1 + "1"



'hello' + 'goodbye'



9 + '99' + 100

"11"

"hellogoodbye"

"999100"

12 / "6" === 2


12 - "6" === 6


12 * "6" === 72


12 + "6" === 18

true

true

true

false

["chris", "kapehe"] + ["mo", "bella"]




["chris", "kapehe"] - ["mo", "bella"]

"chris,kapehemo,bella"

NaN

new Array(3).toString() === ",,"




',,' == ['', '', '']




',,' === ['', '', '']

true

true

false




[1, 2, 3] == [1, 2, 3]

false

k. thx. bai.

Chris on Code

@chrisoncode

scotch.io - Digitalocean.com

JS is Weird

By Chris Sevilleja

JS is Weird

  • 796