Show Menu
Cheatography

Dive into the dynamic world of JavaScript with this concise cheat sheet. Packed with fundamental concepts, code snippets, and useful tips, it's designed to be a quick and concise sidekick in your programming journey.

Comments

// this is a single line comment
/* this is a multi-line comment */
This code will be ignored. Comments are generally a bad idea, your code should be explicit enough as it is.
More info

Variable creation

let variable = "some value";
let num = 4;
let isTrue = true;

Variable operations

let x = 2;
let y = 3;
let z = x + y;
 
let city = "­Rom­e";
let country = "­Ita­ly";
let place = city + " " + country; // Rome Italy

Variable data types

let age = 92; // Number
let name = "­Tom­mas­o"; // String
let canCode = true; // Boolean

Structure types

let students = ["Sa­lva­tor­e", "­Leo­nar­do", "­Ant­one­lla­"]; // Array
let kate = { firstName: "­Mar­ian­na", lastName: "Di Luna", age: 23, canCode: true, }; // Object

Alert

alert(­"this is an alert)­";
let name = "­Jac­k";
alert(­name);

Prompt

let firstName = prompt­("What's your first name?");
let lastName= prompt­("What's your last name?");
let fullName = firstName + " " + lastName;
alert(­ful­lName);

If else statement

let age = prompt­("How old are you?");

if (age < 18) {
alert(­"You cannot apply");
} else {
alert(­"You can apply");
}

Logical or

if (age < 18 || gender === "­mal­e") {
alert(­"You can't do that");
}
The code will be executed if one statement is true.
More info

Logical and

if (continent === "­Eur­ope­" && language === "­Por­tug­ues­e") {
alert(­"You are from Portugal 🇵🇹");
} else {
alert(­"You are not from Portug­al");
}
The code will be executed if both statements are true.
More info

Comparison and logical operators

2 > 3 // false  
2 < 3 // true
2 <= 2 // true
3 >= 2 // true
2 === 5 // false
2 !== 3 // true
1 + 2 === 4 // false

Get Date-Time info

let now = new Date();
now.getMinutes(); // 0,1,2, 12
now.getHours(); //1, 2, 3, 4
now.getDate(); //1, 2, 3, 4
now.getDay(); // 0, 1, 2
now.getMonth(); // 0, 1, 2
now.getFullYear(); // 2021
 

Array

Initia­liz­ation
let numbers = [5, 10, 15, 20];
Add
number­s.p­ush­(25);
Set
numbers[2] = 30;
Delete
delete number­s[1];
Unshift
number­s.u­nsh­ift(1);
Check Existance
number­s.i­ncl­ude­(25);
Pop
let lastNumber = number­s.p­op(); // Remove the last item
Shift
let remove­dNumber = number­s.s­hift(); // Remove the first item
Get Index
let index = number­s.i­nde­xOf­(15);
Loop
for (let num of numbers) {  
consol­e.l­og(­num);
}
Clear
numbers = [];

List

Declar­ation
let temper­atures = new List():
Add
temperatures.add(22);
temperatures.add(25);
temperatures.add(27);
temperatures.add(20);
Set
temper­atu­res.set(2, 26);
Get Item At Index
let temp = temper­atu­res.ge­t(1);
Check Existance
temper­atu­res.co­nta­ins­(27);
Remove
temper­atu­res.re­mov­e(20);
Size
temera­tur­es.s­ize();
Loop
for (let temp of temper­atu­res.to­Arr­ay()) {
consol­e.l­og(­temp);
}
Clear
temper­atu­res.cl­ear();

HashSet

Declar­ation
let travel­Des­tin­ations = new HashSet();
Add
travelDestinations.add("Paris");
travelDestinations.add("Tokyo");
travelDestinations.add("New York");
travelDestinations.add("Sydney");
Values
travel­Des­tin­ati­ons.va­lues();
Delete
travel­Des­tin­ati­ons.de­let­e("S­ydn­ey");
Size
travel­Des­tin­ati­on.s­ize();
Loop
for (let city of travel­Des­tin­ati­ons.va­lues()) {
consol­e.l­og(­`Travel to: ${city}`);
}
Clear
travel­Des­tin­ati­on.c­le­ar();

Queue

Declar­ation
let bankQueue = new Queue();
Enqueue
bankQueue.enqueue("John");
bankQueue.enqueue("Emma");
bankQueue.enqueue("Lucas");
bankQueue.enqueue("Sophia");
Dequeue
bankQu­eue.de­que­ue();
Front
bankQu­eue.fr­ont(); // Get the front of the queue
Check Existance
bankQu­eue.co­nta­ins­("Em­ma");
Size
bankQu­eue.si­ze();

LinkedList

Declar­ation
let train = new Linked­List();
Append
train.a­pp­end­("Engine Car");
train.append("Passenger Car 1");
train.append("Dining Car");
train.append("Passenger Car 2");
train.append("Cargo Car");
Head
let car = train.g­et­Head();
Get At Index
let car = train.g­et­At(2);
Add At Index
train.a­dd­At(3, "­Luxury Car");
Prepend
train.p­re­pen­d("Pilot Car"); // Add at the front
Remove
train.r­em­ove­("Dining Car");
Remove At Index
train.r­em­ove­At(2);
Contains
train.c­on­tai­ns(­"­Dining Car");
Loop
for (let car of train) {
consol­e.l­og(­`In­spe­cting car: ${car}`);
}
 

Dictionary

Declar­ation
let bookAu­thors = new Dictio­nary();
Add
bookAu­tho­rs.a­dd­("Harry Potter­", "J.K. Rowling");
bookAuthors.add("The Hobbit­", "­J.R.R. Tolkien");
bookAuthors.add("Dune", "­Frank Herber­t");
Check Existance Key
bookAu­tho­rs.h­as­Key­("Du­ne");
Check Existance Value
bookAu­tho­rs.h­as­Val­ue(­"J.K. Rowlin­g");
Remove Pair
bookAu­tho­rs.r­em­ove­("The Hobbit­");
Keys
bookAu­tho­rs.k­eys();
Values
bookAu­tho­rs.v­al­ues();
Entries
bookAu­tho­rs.e­nt­ries();
Size
bookAu­tho­rs.s­ize();
Clear
bookAu­tho­rs.c­le­ar();

SortedList

Initia­liz­ation with custom comparator
const byPage­Count = (a, b) => a.pages - b.pages;
let bookshelf = new Sorted­Lis­t(b­yPa­geC­ount);
Add
let book1 = { title: "­Short Storie­s", pages: 150 };
let book2 = { title: "­Nov­el", pages: 320 };
let book3 = { title: "­Poe­ms", pages: 80 };
let book4 = { title: "Long Novel", pages: 480 };
bookshelf.add(book1);
bookshelf.add(book2);
bookshelf.add(book3);
Get At Index
let select­edBook = booksh­elf.ge­t(0);
Contains
booksh­elf.co­nta­ins­(bo­ok3);
Remove
booksh­elf.re­mov­e(b­ook3);
Min
let smalle­stBook = booksh­elf.min();
Max
let larges­tBook = booksh­elf.max();
Size
booksh­elf.si­ze();
Loop
for (let book of booksh­elf.to­Arr­ay()) {
consol­e.l­og(­`${­boo­k.t­itle} (${boo­k.p­ages} pages)`);
}
Clear
booksh­elf.cl­ear();

SortedSet

Initia­liz­ation with a custom comparator
const byScore = (a, b) => a - b;
let examScores = new Sorted­Set­(by­Score);
Add
examScores.add(92);
examScores.add(85);
examScores.add(78);
examScores.add(92);
Check Existance
examSc­ore­s.h­as(85);
Delete
examSc­ore­s.d­ele­te(78);
Min
examSc­ore­s.m­in();
Max
examSc­ore­s.m­ax();
Size
examSc­ore­s.s­ize();
Loop
for (let score of examSc­ore­s.t­oAr­ray()) {
consol­e.l­og(­`Exam score: ${scor­e}`);
}
Clear
examSc­ore­s.c­lear();

Stack

Declar­ation
let browsi­ngH­istory = new Stack();
Push
browsingHistory.push("https://www.example.com");
browsingHistory.push("https://www.google.com");
browsingHistory.push("https://www.stackoverflow.com");
Peek
let first = browsi­ngH­ist­ory.pe­ek();
Pop
let item = browsi­ngH­ist­ory.pop();
Size
browsi­ngH­ist­ory.si­ze();
           
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          JavaScript Cheat Sheet
          jasmine JS testing Cheat Sheet