DEV Community

Cover image for JavaScript Cheatsheet
VedVyas
VedVyas

Posted on

JavaScript Cheatsheet

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 or false
  • 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 
}
Enter fullscreen mode Exit fullscreen mode
  • Call a function:
functionName(argument1, argument2);
Enter fullscreen mode Exit fullscreen mode

Conditionals

  • if statement:
if (condition) { 
  // code to be executed if condition is true 
}
Enter fullscreen mode Exit fullscreen mode
  • if-else statement:
if (condition) { 
  // code to be executed if condition is true 
} else { 
  // code to be executed if condition is false 
}
Enter fullscreen mode Exit fullscreen mode
  • ternary operator:
condition ? expressionIfTrue : expressionIfFalse
Enter fullscreen mode Exit fullscreen mode

Loops

  • for loop:
for (initialization; condition; increment/decrement) { 
  // code to be executed 
}
Enter fullscreen mode Exit fullscreen mode
  • while loop:
while (condition) { 
  // code to be executed 
}
Enter fullscreen mode Exit fullscreen mode
  • do-while loop:
do { 
  // code to be executed 
} while (condition);
Enter fullscreen mode Exit fullscreen mode
  • for-of loop:
for (let element of myArray) { 
  // code to be executed for each element 
}
Enter fullscreen mode Exit fullscreen mode

DOM Manipulation

  • Get an element by ID:
let element = document.getElementById("idName");
Enter fullscreen mode Exit fullscreen mode
  • Get elements by class name:
let elements = document.getElementsByClassName("className");
Enter fullscreen mode Exit fullscreen mode
  • Get elements by tag name:
let elements = document.getElementsByTagName("tagName");
Enter fullscreen mode Exit fullscreen mode
  • Add an event listener:
element.addEventListener("eventName", function() { 
  // code to be executed 
});
Enter fullscreen mode Exit fullscreen mode
  • Change the inner HTML of an element:
element.innerHTML = "new text";
Enter fullscreen mode Exit fullscreen mode
  • Change the style of an element:
element.style.propertyName = "value";
Enter fullscreen mode Exit fullscreen mode

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)