JavaScript is a popular programming language used to create interactive web pages and web applications. Here is a quick cheatsheet of some of the most commonly used syntax and concepts in JavaScript:
Variables
- Declare a variable:
let variableName = value; or const constantName = value;
- Change the value of a variable:
variableName = newValue;
Data Types
- String:
"Hello World"
- Number:
42
- Boolean:
true
orfalse
- Array:
let myArray = [1, 2, 3];
- Object:
let myObject = {name: "John", age: 30};
Operators
- Assignment operator:
=
- Arithmetic operators:
+
,-
,*
,/
,%
- Comparison operators:
==
,===
,!=
,!==
,<
,>
,<=
,>=
- Logical operators:
&&
,||
,!
Functions
- Declare a function:
function functionName(parameter1, parameter2) {
// code to be executed
}
- Call a function:
functionName(argument1, argument2);
Conditionals
- if statement:
if (condition) {
// code to be executed if condition is true
}
- if-else statement:
if (condition) {
// code to be executed if condition is true
} else {
// code to be executed if condition is false
}
- ternary operator:
condition ? expressionIfTrue : expressionIfFalse
Loops
- for loop:
for (initialization; condition; increment/decrement) {
// code to be executed
}
- while loop:
while (condition) {
// code to be executed
}
- do-while loop:
do {
// code to be executed
} while (condition);
- for-of loop:
for (let element of myArray) {
// code to be executed for each element
}
DOM Manipulation
- Get an element by ID:
let element = document.getElementById("idName");
- Get elements by class name:
let elements = document.getElementsByClassName("className");
- Get elements by tag name:
let elements = document.getElementsByTagName("tagName");
- Add an event listener:
element.addEventListener("eventName", function() {
// code to be executed
});
- Change the inner HTML of an element:
element.innerHTML = "new text";
- Change the style of an element:
element.style.propertyName = "value";
This is just a small taste of what you can do with JavaScript. With practice, you can create dynamic and engaging web applications using this powerful language.
You can learn more on Javascript on https://www.vedvyas.io/topics/javascript
Top comments (0)