Js Cheatsheet PDF
Js Cheatsheet PDF
JavaScript Cheatsheet
Page 1
{
Variable Operator
1. String "Any text"
A named reference to Operators are reserved-words that
Six Primitive Types
{
operators that do a
7. Object { key: 'value'} task is a statement.
Keyword / reserved word
- Array [1, "text", false] Any word that is part of the Expression
- Function function name() { } vocabulary of the programming A reference, value or a group
language is called a keyword of reference(s) and value(s)
(a.k.a reserved word). combined with operator(s),
3 Object Examples: var = + if for... which result in a single value.
iLoveCoding
"Don't just learn JavaScript - Become a Full-Stack JavaScript Developer" https://iLoveCoding.org
< > iLoveCoding
♥
JavaScript Cheatsheet
Page 2
4 Function
Parameters / Arguments
A function is simply a bunch of code bundled in a section. This bunch of code ONLY runs when the (optional)
function is called. Functions allow for organizing code into sections and code reusability. A function can optionally
take parameters (a.k.a
Using a function has ONLY two parts. (1) Declaring/defining a function, and (2) using/running a function. arguments). The
function can then use
this information within
Name of function // Function declaration / Function statement
the code it has.
Thats it, its just a name
function someName(param1, param2){
you give to your function.
Tip: Make your function
names descriptive to what Code block
// bunch of code as needed... Any code within the curly
the function does.
var a = param1 + "love" + param2; braces { ... } is called a
"block of code", "code
return a;
block" or simply "block".
Return (optional)
} This concept is not just
A function can optionally
limited to functions. "if
spit-out or "return" a value
statements", "for loops"
once its invoked. Once a // Invoke (run / call) a function and other statements
function returns, no further
someName("Me", "You") use code blocks as well.
lines of code within the
function run.
iLoveCoding
"Don't just learn JavaScript - Become a Full-Stack JavaScript Developer" https://iLoveCoding.org
< > iLoveCoding
♥
JavaScript Cheatsheet
Page 3
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.)
JavaScript Cheatsheet
Page 4
JavaScript Cheatsheet
Page 5
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
"Don't just learn JavaScript - Become a Full-Stack JavaScript Developer" https://iLoveCoding.org
< > iLoveCoding
♥
JavaScript Cheatsheet
Page 6
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.
JavaScript Cheatsheet
Page 7
12 Event Loop
op
e n t Lo
Ev
JavaScript Engine
Call Stack
Last-in - first-out
iLoveCoding
"Don't just learn JavaScript - Become a Full-Stack JavaScript Developer" https://iLoveCoding.org
< > iLoveCoding
♥
JavaScript Cheatsheet
Page 8
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
"Don't just learn JavaScript - Become a Full-Stack JavaScript Developer" https://iLoveCoding.org
< > iLoveCoding
♥
JavaScript Cheatsheet
Page 9
JavaScript Cheatsheet
Page 10
iLoveCoding
"Don't just learn JavaScript - Become a Full-Stack JavaScript Developer" https://iLoveCoding.org
< > iLoveCoding
♥
JavaScript Cheatsheet
Page 11
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
"Don't just learn JavaScript - Become a Full-Stack JavaScript Developer" https://iLoveCoding.org
< > iLoveCoding
♥
JavaScript Cheatsheet
Page 12
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
"Don't just learn JavaScript - Become a Full-Stack JavaScript Developer" https://iLoveCoding.org
< > iLoveCoding
♥
JavaScript Cheatsheet
Page 13
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
"Don't just learn JavaScript - Become a Full-Stack JavaScript Developer" https://iLoveCoding.org