Javascript Cheatsheet | Javascript Explain Very East Steps
Here is a JavaScript cheat sheet that you can use as a quick reference guide:
Variables
Declare variables using let
or const
:
let x; // declares a variable called x const y = 5; // declares a constant called y and assigns it the value 5
Data Types
JavaScript has the following data types:
- String (e.g.
"hello"
) - Number (e.g.
42
) - Boolean (
true
orfalse
) - Null (a special value that represents the absence of a value)
- Undefined (a special value that represents a variable that has not been assigned a value)
- Object (a collection of key-value pairs)
- Symbol (a unique and immutable data type)
Operators
JavaScript has the following operators:
- Arithmetic operators (e.g.
+
,-
,*
,/
,%
) - Comparison operators (e.g.
==
,!=
,<
,>
,<=
,>=
) - Logical operators (e.g.
&&
,||
,!
)
Conditional Statements
Use if
statements to execute different code blocks based on a condition:
if (x > y) { console.log("x is greater than y"); } else if (x < y) { console.log("x is less than y"); } else { console.log("x is equal to y"); }
Loops
Use for
loops to iterate over a block of code a specific number of times:
for (let i = 0; i < 10; i++) { console.log(i); // prints 0 through 9 }
Use while
loops to iterate over a block of code as long as a certain condition is true:
let i = 0; while (i < 10) { console.log(i); // prints 0 through 9 i++; }
Use do...while
loops to execute a block of code at least once before checking a condition:
let i = 0; do { console.log(i); // prints 0 through 9 i++; } while (i < 10);
Functions
Declare functions using the function
keyword:
function greet(name) { console.log("Hello, " + name); } greet("John"); // prints "Hello, John"
Objects
Declare objects using object literals:
const person = { name: "John", age: 30, speak: function() { console.log("Hello, my name is " + this.name); } }; person.speak(); // prints "Hello, my name is John"
Arrays
Declare arrays using square brackets:
const numbers = [1, 2, 3, 4, 5]; console.log(numbers[0]); // prints 1