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";
|
|
|
Variable operations
|
|
|
|
|
|
let place = city + " " + country; // Rome Italy
|
Variable data types
|
let name = "Tommaso"; // String
|
let canCode = true; // Boolean
|
Structure types
let students = ["Salvatore", "Leonardo", "Antonella"]; // Array
|
let kate = { firstName: "Marianna", lastName: "Di Luna", age: 23, canCode: true, }; // Object |
Alert
alert("this is an alert)";
|
|
|
Prompt
let firstName = prompt("What's your first name?");
|
let lastName= prompt("What's your last name?");
|
let fullName = firstName + " " + lastName;
|
|
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 === "male") { alert("You can't do that"); }
|
The code will be executed if one statement is true.
More info
Logical and
if (continent === "Europe" && language === "Portuguese") { alert("You are from Portugal 🇵🇹"); } else { alert("You are not from Portugal"); }
|
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
Initialization let numbers = [5, 10, 15, 20];
|
|
|
Delete delete numbers[1];
|
Unshift numbers.unshift(1);
|
Check Existance numbers.include(25);
|
Pop let lastNumber = numbers.pop(); // Remove the last item
|
Shift let removedNumber = numbers.shift(); // Remove the first item
|
Get Index let index = numbers.indexOf(15);
|
Loop for (let num of numbers) { console.log(num); }
|
|
List
Declaration let temperatures = new List():
|
Add temperatures.add(22); temperatures.add(25); temperatures.add(27); temperatures.add(20);
|
Set temperatures.set(2, 26);
|
Get Item At Index let temp = temperatures.get(1);
|
Check Existance temperatures.contains(27);
|
Remove temperatures.remove(20);
|
Size temeratures.size();
|
Loop for (let temp of temperatures.toArray()) { console.log(temp); }
|
Clear temperatures.clear();
|
HashSet
Declaration let travelDestinations = new HashSet();
|
Add travelDestinations.add("Paris"); travelDestinations.add("Tokyo"); travelDestinations.add("New York"); travelDestinations.add("Sydney");
|
Values travelDestinations.values();
|
Delete travelDestinations.delete("Sydney");
|
Size travelDestination.size();
|
Loop for (let city of travelDestinations.values()) { console.log(`Travel to: ${city}`); }
|
Clear travelDestination.clear();
|
Queue
Declaration let bankQueue = new Queue();
|
Enqueue bankQueue.enqueue("John"); bankQueue.enqueue("Emma"); bankQueue.enqueue("Lucas"); bankQueue.enqueue("Sophia");
|
Dequeue bankQueue.dequeue();
|
Front bankQueue.front(); // Get the front of the queue
|
Check Existance bankQueue.contains("Emma");
|
|
LinkedList
Declaration let train = new LinkedList();
|
Append train.append("Engine Car"); train.append("Passenger Car 1"); train.append("Dining Car"); train.append("Passenger Car 2"); train.append("Cargo Car");
|
Head let car = train.getHead();
|
Get At Index let car = train.getAt(2);
|
Add At Index train.addAt(3, "Luxury Car");
|
Prepend train.prepend("Pilot Car"); // Add at the front
|
Remove train.remove("Dining Car");
|
Remove At Index train.removeAt(2);
|
Contains train.contains("Dining Car");
|
Loop for (let car of train) { console.log(`Inspecting car: ${car}`); }
|
|
|
Dictionary
Declaration let bookAuthors = new Dictionary();
|
Add bookAuthors.add("Harry Potter", "J.K. Rowling"); bookAuthors.add("The Hobbit", "J.R.R. Tolkien"); bookAuthors.add("Dune", "Frank Herbert");
|
Check Existance Key bookAuthors.hasKey("Dune");
|
Check Existance Value bookAuthors.hasValue("J.K. Rowling");
|
Remove Pair bookAuthors.remove("The Hobbit");
|
Keys bookAuthors.keys();
|
Values bookAuthors.values();
|
Entries bookAuthors.entries();
|
Size bookAuthors.size();
|
Clear bookAuthors.clear();
|
SortedList
Initialization with custom comparator const byPageCount = (a, b) => a.pages - b.pages; let bookshelf = new SortedList(byPageCount);
|
Add let book1 = { title: "Short Stories", pages: 150 }; let book2 = { title: "Novel", pages: 320 }; let book3 = { title: "Poems", pages: 80 }; let book4 = { title: "Long Novel", pages: 480 }; bookshelf.add(book1); bookshelf.add(book2); bookshelf.add(book3);
|
Get At Index let selectedBook = bookshelf.get(0);
|
Contains bookshelf.contains(book3);
|
Remove bookshelf.remove(book3);
|
Min let smallestBook = bookshelf.min();
|
Max let largestBook = bookshelf.max();
|
|
Loop for (let book of bookshelf.toArray()) { console.log(`${book.title} (${book.pages} pages)`); }
|
Clear bookshelf.clear();
|
SortedSet
Initialization with a custom comparator const byScore = (a, b) => a - b; let examScores = new SortedSet(byScore);
|
Add examScores.add(92); examScores.add(85); examScores.add(78); examScores.add(92);
|
Check Existance examScores.has(85);
|
Delete examScores.delete(78);
|
|
|
Size examScores.size();
|
Loop for (let score of examScores.toArray()) { console.log(`Exam score: ${score}`); }
|
Clear examScores.clear();
|
Stack
Declaration let browsingHistory = new Stack();
|
Push browsingHistory.push("https://www.example.com"); browsingHistory.push("https://www.google.com"); browsingHistory.push("https://www.stackoverflow.com");
|
Peek let first = browsingHistory.peek();
|
Pop let item = browsingHistory.pop();
|
Size browsingHistory.size();
|
|
Created By
Metadata
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets