undefined
object
boolean
number
string
symbol
function
dot notation
.
brackets notation
[]
console.log(typeof a);
var a;
"undefined"
console.log(typeof "hello world");
"string"
console.log(typeof false);
"boolean"
console.log(typeof 21);
"number"
console.log(typeof null);
"object"
console.log(typeof undefined);
"undefined"
console.log(typeof { name: "John" });
"object"
console.log(typeof typeof 42);
"string"
console.log(typeof 3.1416);
"number"
console.log(typeof "10");
"string"
var person = {
name: "Peter",
age: 40
};
console.log(typeof person.name);
console.log(typeof person.age);
console.log(typeof person.lastName);
"string"
"number"
"undefined"
console.log(typeof []);
"object"
console.log(typeof ["Hello", 20, true][2]);
"boolean"
console.log(typeof ["Hello", 20, true][3]);
"undefined"
console.log(typeof { a: 2 }["a"]);
"number"
var index = "c";
console.log({ a: 1, b: 2, c: 3 }[index]);
3
console.log({ x: 100, y: 200 }.x);
100
function getX (point) {
return point.x;
}
getX.PI = '3.1416';
console.log(typeof getX);
console.log(typeof getX({ x: 34, y: 54 }));
console.log(typeof getX.PI);
"function"
"number"
"string"
var n = "47";
console.log(typeof n);
console.log(typeof Number(n));
"string"
"number"
var n = "47";
console.log(typeof (n * 2));
"number"
var a = "13";
var b = a * 2;
console.log(a); // "13"
console.log(b); // 26
implicit
var a = "13";
var b = Number(a);
console.log(a); // "13"
console.log(b); // 13
explicit
""
or''
empty string
0
,-0
,NaN
zero, -zero, not a number
null
object
undefined
undefined
false
boolean
Boolean('');
false
Boolean('.');
true
Boolean("");
false
Boolean(0);
false
Boolean("0");
true
Boolean(1);
true
Boolean(1/0);
true
Boolean(0/1);
false
Boolean(NaN);
false
Boolean(Infinity);
true
Boolean(0 + "0");
true
Boolean("0" + 0);
true
Boolean("0" * 1);
false
Boolean(1 * "0");
false
Boolean(-1 * 0);
false
Boolean(null);
false
Boolean(undefined);
false
Boolean(false);
false
Boolean(true);
true
Boolean([]);
true
Boolean([1, 2, 3]);
true
Boolean([0].toString());
true
Boolean([1, '', {}][1]);
false
Boolean([1, '', { n: 0 }][2].n);
false
Boolean([].toString());
false
Boolean({});
true
Boolean({}.toString());
true
Boolean({ name: "Doe" });
true
Boolean({ toString: function () {
return '';
}}.toString());
false
Boolean({ number: 0 }.number);
false
Boolean({ char: 'a' }.char);
true
Boolean(function noop() {});
true
==
===
!=
!==
"12" == 12;
true
12 === "12";
false
1 == true;
true
true === 1;
false
false == 0;
true
false == "false";
false
"" == false;
true
null == false;
false
undefined == null;
true
false == undefined;
false
0 == "";
true
0 == null;
false
var a;
a == null;
true
({} == {});
false
({} === {});
false
[] == [];
false
[1, 2, 3] === [1, 2, 3];
false
[1, 2, 3] == "1,2,3";
true
NaN == NaN;
false
NaN === NaN;
false
(function noop (){}) == (function noop (){});
false
(function noop (){}).toString() == (function noop (){});
true
2 > "1";
true
"a" < "b";
true
3 < "a";
false
"3" < "a";
true
0 > NaN;
false
identifier | is valid? |
---|---|
Name |
true |
0duck |
false |
last.name |
false |
$account |
true |
_age |
true |
-price |
false |
car[123] |
false |
for |
false |
when a
var
declaration is conceptually "moved" to the top of its enclosing scope.
a = 10;
foo ();
function foo () {
a = 5;
console.log(a);
var a;
}
var a;
console.log(a);
5
10
var
declares variables at function level
let
declares variables at block level
var a = 1;
function foo () {
if (a == 1) {
var b = 2;
}
console.log(b);
}
foo();
2
var a = 1;
function foo () {
if (a == 1) {
let b = 2;
}
console.log(b);
}
foo();
Uncaught ReferenceError: b is not defined
function foo () {
var a = 10
}
foo();
console.log(a);
Uncaught ReferenceError: a is not defined
function foo () {
a = 20
}
foo();
console.log(a);
20
18. Write the code to log if a number is even
or odd
using the if
statement, switch
statement and the conditional operator ?:
aka ternay operator
Clue: you can determine if a number is even
if the remainder of n
divided by 2
is equal to 0
. Use the remainder operator %
.
if
Solution:
var n = 10;
if (n % 2 == 0) {
console.log('even');
} else {
console.log('odd');
}
switch
Solution:
var n = 10;
switch (n % 2) {
case 0:
console.log('even');
break;
default:
console.log('odd');
}
ternary
Solution:
var n = 10;
console.log(n % 2 == 0 ? "even" : "odd")
is a way to force code to be better written.
is a way to opt in to a restricted variant of JS.
is a way to avoid bugs.
is the opposite of
sloppy mode
.
function yummy () {
a = 50
}
yummy();
console.log(a);
50
'use strict';
function yummy () {
a = 50
}
yummy();
console.log(a);
Uncaught ReferenceError: a is not defined
Functions are the primary mechanism of _____ in JS.
scope
22. Create a function square
that takes one parameter number
that returns the result of that number
multiplied by itself. You need to perform this in 3
different ways
Solution:
function square (number) {
return number * number;
}
Solution:
var square = function (number) {
return number * number;
};
Solution:
var square = function square (number) {
return number * number;
};
an Immediately Invoked Function Expression
example:
(function hello () {
console.log("hello!");
})();
a
closure
is the combination of a function and the lexical environment within which that function was declared.
function createHello (greeting) {
return function greet (name) {
return greeting + " " + name + "!";
}
}
var greetInSpanish = createHello('Hola');
console.log(greetInSpanish("Pedro"));
console.log(createHello('Hi')("Mike"));
"Hola Pedro!"
"Hi Mike!"
// Kitties
function sayHello() {
console.log("Hello " + this.name);
}
var name = "Fluffy";
sayHello();
var kitty = {
name: "Buttercup",
sayHello: sayHello
};
kitty.sayHello();
sayHello.call({ name: "Cocoa" });
new sayHello();
"Hello Fluffy"
"Hello Buttercup"
"Hello Cocoa"
"Hello undefined"
var animal = {
walk: "I'm walking!"
};
var bird = Object.create(animal);
bird.fly = "I'm flying!";
var penguin = Object.create(bird);
penguin.fly = "I can't fly :(";
penguin.swim = "I'm swimming!";
var canary = Object.create(bird);
var tiger = Object.create(animal);
console.log(tiger.walk);
console.log(tiger.fly);
console.log(canary.fly);
console.log(canary.walk);
console.log(canary.swim);
console.log(penguin.fly);
console.log(penguin.walk);
console.log(penguin.swim);
"I'm walking!"
"undefined"
"I'm flying!"
"I'm walking!"
"undefined"
"I can't fly: :("
"I'm walking!"
"I'm swimming!"
28. What are the two main techniques to make older browsers work with newer features available in JS? Describe each
polyfilling. A piece of code that provides the techonology that you expect the browser to provide natively.
transpiling. The process that converts your newer code into older code equivalents.
nope,
alert
is provided by the browser.
2.1 Create a module in an IIFE
stored in a variable calculator
that exposes 4 methods: plus
, minus
, times
and dividedBy
. Each method should receive one parameter firstNumber
and return a function that receives another parameter secondNumber
. Every method should perform the operation it describes, for example, it's expected that plus
adds the firstNumber
to the secondNumber
, dividedBy
should divide the secondNumber
by the firstNumber
and so on. The idea is to have "stored" the firstNumber
to perform the next operations with it.
TIP: You can create a script.js
file and test your code in the browser or node.js.
Solution:
// create your calculator module here
var calculator = (function calc () {
function plus (firstNumber) {
return function addTo (secondNumber) {
return secondNumber + firstNumber;
}
}
function minus (firstNumber) {
return function subtractTo (secondNumber) {
return secondNumber - firstNumber;
}
}
function times (firstNumber) {
return function multiplyTo (secondNumber) {
return secondNumber * firstNumber;
}
}
function dividedBy (firstNumber) {
return function divideTo (secondNumber) {
return secondNumber / firstNumber;
}
}
return {
plus: plus,
minus: minus,
times: times,
dividedBy: dividedBy
};
})();
// Do NOT touch this code
const testCases = [
{ type: "plus", n1: 3, n2: [1, 2, 3], expected: [4, 5, 6] },
{ type: "minus", n1: 1, n2: [1, 0, -1], expected: [0, -1, -2] },
{ type: "times", n1: 5, n2: [2, 5, 10], expected: [10, 25, 50] },
{ type: "dividedBy", n1: 10, n2: [10, 100, 1000], expected: [1, 10, 100] }
];
const result = testCases.every(test => {
const operation = calculator[test.type](test.n1);
return test.n2.every((number, i) => {
const testPassed = operation(number) === test.expected[i];
if (!testPassed) {
console.log(`Expected: ${operation(number)} to be: ${test.expected[i]}. Operation: secondNumber(${number}) ${test.type} firstNumber(${test.n1})`);
}
return testPassed;
});
});
console.log("All tests passed: ", result);