Cheatography
https://cheatography.com
JavaScript - Basics Concepts
This is a draft cheat sheet. It is a work in progress and is not finished yet.
Variables
|
block scope |
most used |
|
constant values |
|
global or function scope |
old usage |
Operators
let x = 10; |
assignment |
x == "Hello" |
comparison |
x === "Hello" |
strict comparison |
&& |
logical AND |
|| |
logical OR |
! |
logical negation |
Data Types
let num = 10; |
Number |
let str = "Hello"; |
String |
let isTrue = true |
Boolean |
let arr = [1, 2, 3] |
Array |
let obj = { a:1, b:2, c:"three" } |
Object |
Array
let array = ["a", "b", "c", "d", "e"] |
array definition |
array.length //5 |
number of elements in array |
array[0] // a |
access the first element |
array[n] // 0 <= n < lenght of array |
access nth element |
array[5] // error |
5 grater then length of array |
array[array.length - 1] // e |
access the last element |
Object usage
object.name // Alice |
dot notation |
object["name"] // Alice |
property access notation |
object.age // 30 |
object.gender // undefined |
non-existent property |
|
|
Functions
function greet() {
console.log("Hello")
}
|
try...catch...finally
try{
//code might throw an error
} catch(err) {
console.error("Error occurred", err.message);
} finally {
console.log("Operation completed");
}
|
try: This block contains code that might throw an exception (an error)
catch: This block handles the exception if an error occurs in the try block
finally: This block always executes, regardless of whether an error occurs or not
Object
let object = {
name: "Alice",
age: 30
}
|
Unlike JSON, a JavaScript object does not have double quotes (") in the property name
|