Javascript Cheat Sheet: Credit - Ilovecoding
Javascript Cheat Sheet: Credit - Ilovecoding
Cheat Sheet
Credit -iLoveCoding
1 Seven (7) Types 2 Basic Vocabulary
{
Variable Operator
1. String "Any text" A named reference to Operators are reserved-words that
Six Primitive Types
{
6. Symbol Symbol('something') task is a statement.
iLoveCoding
5 Vocabulary around variables and scope
Variable Declaration Scope
var a; The creation of the The limits in which a variable exists. var a = "global";
variable.
function first(){
Global scope
Variable Initialization The outer most scope is called the Global var a = "fresh";
a = 12; The initial scope.
assignment of value
function second(){
to a variable.
Functional scope console.log(a);
Any variables inside a function is in scope }
a = "me"; Variable Assignment of the function.
}
Assigning value to a
variable.
Lexical Environment (Lexical scope) Scope chain
The physical location (scope) where a The nested hierarchy of scope is
console.log(a); Hoisting variable or function is declared is its lexical called the scope chain. The JS
Variables are environment (lexical scope). engine looks for variables in the
var a = "me";
declared at the top scope chain upwards (it its
of the function Rule: ancestors, until found)
automatically, and (1) Variables in the outer scope can be
initialized at the time accessed in a nested scope; But variables
they are run. inside a nested scope CANNOT be accessed
by the outer scope. (a.k.a private variables.)
break;
Ternary Operator: A ternary operator returns
the first value if the expression is truthy, or
else returns the second value. default:
// run this code
(expression)? ifTrue: ifFalse; }
iLoveCoding
10 Loop Statements
Loops are used to do something repeatedly. For instance lets say we get a list of 50 Step 1: Run the initial expression.
blog posts from the database and we want to print their titles on our page. Instead of
writing the code 50 times, we would instead use a loop to make this happen. Step 2: Check if condition meets. If
condition meets, proceed; or else end the
loop.
For loop
Step 3: Run the code in block.
for (initial-expression; condition; second-expression){
// run this code in block Step 4: Run the second-expression.
}
Step 5: Go to Step 2.
While loop
Step 1: If the condition is true, proceed; or
while (i<3){ else end the loop.
12 Event Loop
o op
ve nt L
E
JavaScript Engine
Call Stack
Last-in - first-out
iLoveCoding
13 Browser
A web browser is a pretty advance piece of software which contains a lot of components. Many of these components are accessible to a
web developer, so we can create complex web apps. At the same time a lot of components are kept out of reach of the web developer for
security purposes. For instance, we as web developers can get access to the user's location, but we cannot get access to the user's saved
passwords or browsing history. Let's see below how a browser is structured:
Dev Tools
iLoveCoding
14 DOM - Document Object Model
What is a "Node"?
Query/Get Elements Modify Element (in the context of DOM)
// Preferred way: node.style.color = 'red'
document.querySelector('css-selectors') node.style.padding = '10px', Node: Every item in the DOM
document.querySelectorAll('css-selectors', ...) node.style.fontSize = '200%' tree is called a node. There
are two types of node - A text
// Old ways, and still work: node.setAttribute('attr-name', 'attr-value') node, and an element node:
document.getElementsByTagName('element-name') node.removeAttribute('attr-name')
document.getElementsByClassName('class-name') Text Node: Node that has text.
document.getElementById('id') Get and Modify Element Class
node.classList Element Node: Node that has
Create / clone Element node.classList.add('class-name', ...) an element.
document.createElement('div') node.classList.remove('class-name', ...)
document.createTextNode('some text here') node.classList.toggle('class-name')
node.cloneNode() node.classList.contains('class-name') Child Node: A node which is a
node.textContent = 'some text here' node.classList.replace('old', 'new') child of another node.
iLoveCoding
17 Promise
What is a Promise? What is an Async task?
Promise is an object that provides a useful construct when dealing An async task is one in which a third-party process is
with asynchronous tasks. A promise is called a "Promise" because it doing the task.
guarantees it will run upon success or failure of that task. Examples:
- Requesting/sending data to a database
Working with a promise consists of two parts; (A) Creating a promise, - Requesting/sending data via HTTP protocol
and (B) Using a promise. - Working with the file system of the computer
iLoveCoding
18 'this' keyword
var name = "Fatema";
The this keyword is used inside a function. The this
keyword is merely a reference to another object.
function fun(){
What the this keyword refers to depends on the // some code here
scenario or the way the function is implemented.
console.log(this.name);
Here are the 3 scenarios to remember:
}
Scenario #1: this inside a function
The this keyword points to global object. const user = {
name: "Marium",
Scenario #2: this inside a method yearOfBirth: 1999,
The this keyword points to the object the calcAge: function(){
method is in.
const currentYear = (new Date()).getFullYear();
Scenario #3: When function is run with return currentYear - this.yearOfBirth;
call, bind or apply }
When a function is called using the }
.call(param) .bind(param) or .apply(param)
method, the first param become the object
that the this keyword refers to. fun(); // 'this' is global. Logs "Fatema"
user.calcAge(); // 'this' is the user object
fun.call(user); // 'this' is the user object. Logs "Marium"
Important Note:
In the browser, global is the window object.
In Node.js, global is the global object.
iLoveCoding
19 Constructor
// Defining a Constructor Rule of thumb:
What is a constructor?
function Car(make, model, year){ A) Set properties
In JavaScript, a constructor is a special
this.make = make; inside a constructor.
function that acts as a mold to create
new objects. this.model = model;
B) Set methods inside
this.year = year; the prototype
There are numerous built-in constructors
property.
in JavaScript, such as String, Number,
Promise, Date, Array, Object, and many this.setMiles = function(miles){
more. this.miles = miles
"new" keyword
return miles; The new keyword is
We can create our own custom
} used to create a new
constructors if need be.
} object (instance) from
the constructor.
A great place to use a constructor is
when you are creating multiple objects of // Using a constructor
the same kind.
const car1 = new Car('Toyota', 'Prius', 2016); "prototype" property
const car2 = new Car('Hyundai', 'Sonata', 2018); prototype is a special
There are two parts to working with a
property on every
constructor:
object. Properties
// Adding method to the constructor prototype (methods or values)
(1) Defining a constructor
Car.prototype.age = function(){ attached to the
When creating a custom constructor
return (new Date()).getFullYear() - this.year; prototype property
get inherited to every
(2) Using a constructor }
instance of the
with the "new" keyword
constructor.
car1.age(); // 2
iLoveCoding