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.
// this is a single line comment |
/* this is a multi-line comment */ |
let x = 2; |
let y = 3; |
let z = x + y; |
let city = "Rome"; |
let country = "Italy"; |
let place = city + " " + country; // Rome Italy |
let age = 92; // Number |
let name = "Tommaso"; // String |
let canCode = true; // Boolean |
let students = ["Salvatore", "Leonardo", "Antonella"]; // Array |
|
let kate = { firstName: "Marianna", lastName: "Di Luna", age: 23, canCode: true, }; // Object
|
let firstName = prompt("What's your first name?"); |
let lastName= prompt("What's your last name?"); |
let fullName = firstName + " " + lastName; |
alert(fullName); |
let age = prompt("How old are you?"); |
if (age < 18 || gender === "male") { |
if (continent === "Europe" && language === "Portuguese") { |
2 > 3 // false |
let now = new Date(); |
|
Initialization
let numbers = [5, 10, 15, 20]; |
|
Add
numbers.push(25); |
|
Set
numbers[2] = 30; |
|
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) { |
|
Clear
numbers = []; |
|
Declaration
let temperatures = new List(): |
|
Add
temperatures.add(22); |
|
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()) { |
|
Clear
temperatures.clear(); |
|
Declaration
let travelDestinations = new HashSet(); |
|
Add
travelDestinations.add("Paris"); |
|
Values
travelDestinations.values(); |
|
Delete
travelDestinations.delete("Sydney"); |
|
Size
travelDestination.size(); |
|
Loop
for (let city of travelDestinations.values()) { |
|
Clear
travelDestination.clear(); |
|
Declaration
let bankQueue = new Queue(); |
|
Enqueue
bankQueue.enqueue("John"); |
|
Dequeue
bankQueue.dequeue(); |
|
Front
bankQueue.front(); // Get the front of the queue |
|
Check Existance
bankQueue.contains("Emma"); |
|
Size
bankQueue.size(); |
|
Declaration
let train = new LinkedList(); |
|
Append
train.append("Engine 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) { |
|
Declaration
let bookAuthors = new Dictionary(); |
|
Add
bookAuthors.add("Harry Potter", "J.K. Rowling"); |
|
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(); |
|
Initialization with custom comparator
const byPageCount = (a, b) => a.pages - b.pages; |
|
Add
let book1 = { title: "Short Stories", pages: 150 }; |
|
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(); |
|
Size
bookshelf.size(); |
|
Loop
for (let book of bookshelf.toArray()) { |
|
Clear
bookshelf.clear(); |
|
Initialization with a custom comparator
const byScore = (a, b) => a - b; |
|
Add
examScores.add(92); |
|
Check Existance
examScores.has(85); |
|
Delete
examScores.delete(78); |
|
Min
examScores.min(); |
|
Max
examScores.max(); |
|
Size
examScores.size(); |
|
Loop
for (let score of examScores.toArray()) { |
|
Clear
examScores.clear(); |
|
Declaration
let browsingHistory = new Stack(); |
|
Push
browsingHistory.push("https://www.example.com"); |
|
Peek
let first = browsingHistory.peek(); |
|
Pop
let item = browsingHistory.pop(); |
|
Size
browsingHistory.size(); |
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.