JavaScript is high-level, often just-in-time compiled, and multi-paradigm programming language. [Wikipedia]
We declare them using let or const ( var is old and should not be used) can hold pretty much anything ranging from values, objects, functions etc.
let- default way of declaring a variable
- can be reassigned
- can be uninitialized
- lifecycle only in the block it is defined in
const- declares a constant
- MUST be initialized with a value when declared
- can't be changed
- lifecycle only in the block it is defined in
varplease don't use this, legacy, deprecated etc.- here just for educational purposes
- does not have block scope its either global or function scope, see below
- JS uses
camelCasefor variables, functions, objects etc.,MACRO_CASEfor constants where we store values to be used throughout our code andPascalCasefor classes and interfaces for example:
const MAIN_COLOR = "#FF7F00";
let mainRect = document.getElementById('rect');
mainRect.style.fill = MAIN_COLOR;
class SleepData{};Always use const unless you need to change the value of what is stored in the variable
- brackets create a code block
{ const a = 3; console.log(a); } const a = 5; console.log(a);
3 5
- functions create a function block
function demoFunction(){ const a = 3; console.log(a); } demoFunction(); const a = 5; console.log(a);
3 5
ifstatements,forloops create a code blockif (true){ const a = 3; console.log(a); } const a = 5; console.log(a); console.log('==='); for (let i=0; i<1; i++){ const a = 2; console.log(a); } console.log(a);
3 5 === 2 5
- var special case, lifespan extends past block its declared in
if (true){ var a = 3; // creates a global variable a console.log(a); } console.log(a);
3 3
Function declarations are "hoisted" up to the start of the block. (let and const are weird here)
greeting();
function greeting() {
console.log("Hello!");
}Hello!Function variables are not
greeting();
var greeting = function greeting(){
console.log("Hello!");
}TypeError: greeting is not a functionWhy? Since it is declared with var its declaration is hoisted to the start of the block but not its implementation, it does not hold a function when called.
- primitives
null // is weird
undefined
boolean
number
string
object
symbol // Added in ES6
- Everything starts as
undefinedwhen declared and not assigned yet.
let a;
console.log(a);undefinedtypeof myVarreturns type ( one of the above exceptnull) of myVar,typeofreturns a string
const a = 42;
console.log(typeof a);
console.log(typeof typeof a);number
string-
why is null weird
typeof nullreturnsObjectinstead ofnulleven though it's one of the base types
-
number- can be integer
42, float42.42, binary0b1011, octal0o377or hexa0xFF - can be
InfinityorNaN, both are not actually numbers, but still part of the number type, weird i know
- can be integer
-
string- used with single, double quotes or backticks
-
booleantrue/false-
if (condition){ //something } else { //something else }
- Has shorthand syntax
typeof null === "object" ? console.log('Yes') : console.log("No")
-
0,- 0, null, NaN, undefined, ""are all false when used in a logical context- anything else is true
==vs===- Double equals only compares values (there is casting being done)
- Triple equals compares both values and types
const a = 42; const b = "42"; console.log(a == b); console.log(a === b);
true false
Array, String, Date, Number, Math, RegExp, Set, Map, Function
- Each of them has their own methods
- Not enought time to talk about all :<
const a = [1,2,3,4,5];
console.log(a.indexOf(3));2- Type that contains data ( even other objects ) and/or functions
- Each key is a string
- Can access with either
myObj.fieldormyObj["field"]const exampleObject = { field : "this is a string", notTruthy: false, exNumber: 42, nestedObject: { nestedField: null } myFunc: function(){ console.log('Hello'); } }
this- special keyword referring to the parent Object of the current executing function, to put it lightly
- one of the harder concepts in JS
const exampleObject = { name: "Gabriel", myFunc: function(){ console.log(`Hi, my name is ${this.name}`); } } exampleObject.myFunc();
Hi, my name is Gabriel
function example1(){
console.log('ex1');
}
const example2 = () => {
console.log('ex2');
}
(() => {
console.log('ex3');
})()
example1();
example2();ex3
ex1
ex2- Why?
- Example 3 is an IIFE ( immediately invoked function expression ), creates and executes the function immediately, was used before to contain
varto not reach the global scope, not much usage nowadays
- Example 3 is an IIFE ( immediately invoked function expression ), creates and executes the function immediately, was used before to contain
- What is an arrow function?
- Example 2 is an arrow function, a short-hand syntax for declaring functions, can be declared with arguments or without, useful in HoF ( high order functions ) for example:
const a = [1,2,3,4,5,6]; console.log( a.filter( (element) => { return element % 2 === 0}) )
filteris a function of theArraynative Object, it accepts a function that returns aboolean, will loop through the array and will return a copy of the original array with filtered values that returnedtruewhen applied the given function- It can accept 2 arguments if provided, first one (shown here) is the current element of the loop, second one ( not provided here ) is the current index
- Different examples for writing arrow functions
const a = () => {/*Do Something*/}; const b = (arg1) => { return arg1.field}; const c = arg1 => {return arg1.field}; const d = arg1 => arg1.field const myObj = { field: "Hi" } console.log(b(myObj)); console.log(c(myObj)); console.log(d(myObj));
Hi Hi Hi
- Note: functions
b, c, dbehave and return the same thing, just written differently, all are valid
- Example 2 is an arrow function, a short-hand syntax for declaring functions, can be declared with arguments or without, useful in HoF ( high order functions ) for example:
- Switch cases
const a = 1; switch(a){ case 1: console.log('a is 1'); break; case 2: console.log('a is 2'); break; default: console.log('a is whatever?'); break; }
a is 1
const a = 1; switch(a){ case 1: console.log('a is 1'); case 2: console.log('a is 2'); default: console.log('a is whatever?'); }
a is 1 a is 2 a is whatever?
- Spread syntax
- Definition: It allows an iterable to expand in places where 0+ arguments are expected.
- Finding max in an array
const arr = [2, 4, 8, 6, 0]; const max = Math.max(...arr); console.log(max);
8
- Inserting into an array
// Without spread syntax const a = [1,2,3,4,5]; const b = [0, a, 6]; console.log(b); // With spread syntax const c = [0, ...a, 6]; console.log(c);
[0,[1,2,3,4,5],6] [0,1,2,3,4,5,6]
- Functions with indefinite number of arguments
function printAll(...args){ args.forEach((arg) => console.log(arg)) }; printAll(1,2,3);
1 2 3
- Object destructuring
const myObj ={ temperature : 12, type: "celsius" } const {temperature, type} = myObj; /* const temp = myObj.temperature const type = myObj.type */ console.log(temperature); console.log(type);
12 celsius
- Error handling
- Try catch blocks ( synchronus )
try{ const a = false; if (a) { console.log('a is true'); } else { throw new Error('a is false, abort'); } }catch(e){ console.error(e) console.log('handle it somehow'); }
Error: "a is false, abort" handle it somehow
- Promise rejects (asynchronus )
- Try catch blocks ( synchronus )
In my opinion one of the most important things about JS. What happens when we need a part of code or data from somewhere that takes time to run and give us that which we need?
Consider the following example:
- We need to grab some data from an external source, lets say a RESTful API that returns the current temperature
- We can't do something like
const data = getData('accuweather'); // getData is a placeholder console.log(data.temperature);
undefined - Why?
- This is because
getDatais an asynchronus method in this example, it takes time for it to get data and return it to our const. Since we made no effort in telling JS this, it executed the function returned nothing ( undefined ) and went on to log it for us
- This is because
- How can we fix it?
- Callbacks
- Callbacks are functions passed as a parameter that execute when called, for example when we receive data from an external source
console.log('I am here'); function serverRequest(site, callback){ // Simulate a delay setTimeout(() => { const response = { temperature: 12 }; callback(response); },5000); } function getResults(results){ console.log("Response from the server: " + results.temperature); } serverRequest("accuweatherapi", getResults); console.log('I am here number 2');
- Actual real world example, nodeJS
readFile
fs.readFile('path',(err, data) => { console.log(`Got data -> ${data}`); })
- Promises
- A
Promiseis a native Object that represents the eventual completion (or failure) of an asynchronous operation, and its resulting value.
console.log('I am here'); function serverRequest(site){ return new Promise((resolve,reject) => { setTimeout(() => { const response = { temperature: 12 }; resolve(response); },5000) }) } serverRequest('accuweatherApi') .then((data) => { console.log(`Got data -> ${data}`) }); console.log('I am here number 2');
- A
- Callbacks
Useful links