JavaScript Cheatsheet
JavaScript Cheatsheet
Use the <script> tag with the src attribute to link <script src="script.js">
External
to an external JavaScript file. </script>
2. Variables
Variables Description Example
Defines a function-scoped variable that can be changed
Var var x = 10;
or redeclared.
3. JavaScript Comments
Comment Description Example
let x = 5; // This is another
Single Line Use two forward slashes //.
single line comment.
4. Data Types
Data Type Description Example
Number Stores numeric values. let age = 30;
5. Variables Scope
Scope Description Example
Local Variables are accessible only within the function. function test() { let x = 10; }
6. Conditional Statement
Statement Description Example
if (condition) {
Executes code block if the condition
If // Code
is true.
}
if (condition) {
// Code
Executes one block if true, and
If-else } else {
another if false.
// Code
}
switch(expression) {
case x:
Executes code blocks based on a
Switch // Code;
matching case.
break;
}
7. Loops
Type Description Example
for (let i = 0; i < 5; i++){
For Repeats code for a set number of times. // Code
}
8. Strings
Type Description Example Output
Replaces a specified pattern let text = "Hello World";
Replace() with another pattern in a console.log(text.replace( Hello Educative
string. "World", "Educative"));
JavaScript Cheatsheet
Replaces a specified pattern let text = "Hello World";
Replace() with another pattern in a console.log(text.replace( Hello Educative
string. "World", "Educative"));
9. Arrays
Type Description Example Output
push() Adds new items to the end let fruits = ["Apple", ["Apple", "Banana"
of an array. "Banana"]; , "Orange"]
fruits.push("Orange");
console.log(fruits);
JavaScript Cheatsheet
let fruits = ["Apple",
Removes the last element
"Banana", "Orange"];
pop() of an array, and returns ["Apple", "Banana"]
fruits.pop();
that element. console.log(fruits);
10. Operators
Type Operator Example Output
AND (&) 5&1 1
OR (|) 5|1 5
14. Date
Function Description Example Output
Returns the minutes (0–59) of a var date = new Date();
getMinutes() date.getMinutes(); (e.g., 45)
specified date and time.
JavaScript Cheatsheet
Returns the day of the month var date = new Date();
getDate() date.getDate(); (e.g., 28)
(1–31) for a specified date.
Returns the day of the week var date = new Date(); (e.g., 2 for
getDay() Tuesday)
(0–6) for a specified date. date.getDay();
16. Events
Model Description Example
Triggers a function when an <button onclick="alert('Button
onclick()
element is clicked. clicked!')">Click me</button>
<form onsubmit="console.log
('Form submitted!')">
Triggers a function when a
onsubmit() <input type="submit" value=
form is submitted.
"Submit">
</form>
<div draggable="true"
Triggers a function when an ondrag="console.log
ondrag() ('Element dragged!')
element is dragged.
">Drag me</div>
17. Error
Model Description Example
try {
Defines a block of code to
Try // Code to try
test for errors.
}
catch (error) {
Defines a block of code to
catch // Code to handle error
handle any errors
}
finally {
Executes code after try
// Code to execute after try
finally and catch, regardless of the
and catch
result.
}