JavaScript cheat sheet for freshers

A JavaScript cheat sheet for freshers offers a concise reference to key JavaScript syntax, functions, and concepts, helping beginners quickly get comfortable with the language. It usually includes basics like declaring variables (let,const,var), data types (strings, numbers, arrays, objects), and common operators.

The cheat sheet often covers control structures such as if, else, switch, for, and while loops, as well as functions (declaration and arrow functions). It may also include array methods like map, filter, reduce, and string manipulation methods, which are commonly used for data processing.

Additionally, there’s often a focus on JavaScript’s unique features, such as the Document Object Model (DOM) manipulation, events, and understanding scope (global vs. local). Freshers might also find sections on asynchronous programming concepts like promises and async/await helpful. A JavaScript cheat sheet is a handy tool to quickly reference syntax, understand core concepts, and boost confidence while learning to code in JavaScript.

1. Basics

Comments

  • Definition: Comments are notes for developers that JavaScript ignores during execution.
  • Explanation: Use // for single-line comments and /* * / for multi-line comments.
// This is a single-line comment
/* This is a multi-line comment */

Printing Output

  • Definition: console.log() is used to print output to the console.
  • Explanation: Useful for debugging or checking variable values.
console.log("Hello, World!");  // Outputs: Hello, World!

Variables

  • Definition: Variables store data like numbers, text, or objects.
  • Explanation: Declare variables with let, const, or Var
let x = 5;            // Can be reassigned
const y = 10; // Cannot be reassigned
var z = 15; // Legacy variable declaration (avoid in modern JavaScript)

2. Data Types and Casting

Common Data Types

  • Definition: Data types specify the kind of value a variable holds.
  • Explanation: JavaScript variables can hold numbers, strings, boolean, objects, etc.
let age = 25;               // Number
let name = "Kaashiv"; // String
let isActive = true; // Boolean
let person = {name: "Bob"}; // Object
let colors = ["red", "blue"]; // Array

Type Conversion

  • Definition: Type conversion changes the data type of a value.
  • Explanation: Use String(), Number(), Boolean() to convert values.
let x = String(123);  // Converts 123 to "123"
let y = Number("5"); // Converts "5" to 5

3. Basic Operators

  • Definition: Operators perform operations on variables and values.
  • Explanation: JavaScript has arithmetic, comparison, and logical operators.
let sum = a + b;           // Addition
let isEqual = a == b; // Comparison
let result = a && b; // Logical AND

4. Strings

  • Definition: Strings are sequences of characters for storing text.
  • Explanation: JavaScript strings have various methods for manipulation, like changing case, finding length, etc.
let greeting = "Hello, World!";
console.log(greeting.toUpperCase()); // Outputs: HELLO, WORLD!

String Concatenation

  • Explanation: Use +  or template literals with ${} for concatenating strings.
let firstName = "Alice";
console.log(`Hello, ${firstName}!`); // Outputs: Hello, Alice!

5. Arrays

  • Definition: Arrays store multiple values in a single variable.
  • Explanation: JavaScript arrays are dynamic and can hold any type of data.
let fruits = ["apple", "banana", "cherry"];
console.log(fruits[0]); // Outputs: apple

Array Methods

  • Explanation: Arrays have built-in methods like push(), pop(), shift(), and map().
fruits.push("orange");    // Adds "orange" to the end
let upperFruits = fruits.map(fruit => fruit.toUpperCase());

6. Objects

  • Definition: Objects are collections of key-value pairs.
  • Explanation: Useful for storing related data or complex structures.
let person = {
name: "Alice",
age: 25,
greet: function() {
console.log("Hello, " + this.name);
}
};
console.log(person.name); // Outputs: Alice

7. Conditional Statements

If-Else

  • Definition: Conditional statements control program flow based on conditions.
  • Explanation: Use if, else if, and else to make decisions in code.
let age = 18;
if (age >= 18) {
console.log("Adult");
} else {
console.log("Minor");
}

Ternary Operator

Explanation: A shorthand for if-else that uses condition ? expression-If-True : expression-If-False.

let status = age >= 18 ? "Adult" : "Minor";

8. Loops

For Loop

  • Definition: for loops run a block of code a set number of times.
  • Explanation: Useful for iterating over arrays or repeating actions.
for (let i = 0; i < 5; i++) {
console.log(i);
}

While Loop

  • Definition: while loops run as long as a condition is true.
  • Explanation: Useful when the number of iterations isn’t predetermined.
let count = 0;
while (count < 5) {
console.log(count);
count++;
}

9. Functions

Function Declaration

  • Definition: Functions are reusable blocks of code that perform specific tasks.
  • Explanation: Define functions with function keyword or as arrow functions.
function greet(name) {
return `Hello, ${name}!`;
}

console.log(greet("Alice")); // Outputs: Hello, Alice!

Arrow Functions

  • Explanation: A shorter syntax for functions introduced in ES6.
const add = (a, b) => a + b;
console.log(add(5, 3)); // Outputs: 8

10. Exception Handling

  • Definition: Exception handling catches and manages errors in code.
  • Explanation: Use try,catch,finally, to handle exceptions gracefully.
try {
let result = 10 / 0;
} catch (error) {
console.log("An error occurred: " + error.message);
} finally {
console.log("This runs regardless of error.");
}

11. Object-Oriented Programming (OOP)

Classes

  • Definition: Classes are blueprints for creating objects.
  • Explanation: Use Class keyword to define classes and instantiate objects.
class Car {
constructor(brand) {
this.brand = brand;
}

drive() {
console.log(`${this.brand} is driving`);
}
}

let myCar = new Car("Toyota");
myCar.drive(); // Outputs: Toyota is driving

Inheritance

  • Definition: Inheritance allows one class to inherit properties and methods from another.
  • Explanation: Use extends to create subclasses.
class ElectricCar extends Car {
charge() {
console.log(`${this.brand} is charging`);
}
}

let myTesla = new ElectricCar("Tesla");
myTesla.charge(); // Outputs: Tesla is charging

12. Promises (Asynchronous JavaScript)

  • Definition: Promises handle asynchronous operations, like loading data from a server.
  • Explanation: A promise can be resolved (successful) or rejected (error).
let myPromise = new Promise((resolve, reject) => {
let success = true;
if (success) {
resolve("Promise resolved!");
} else {
reject("Promise rejected.");
}
});

myPromise
.then(response => console.log(response))
.catch(error => console.log(error));

13. Document Object Model (DOM) Manipulation

  • Definition: The DOM represents the structure of a webpage.
  • Explanation: JavaScript can manipulate HTML elements through the DOM.
document.getElementById("myElement").innerHTML = "Hello, World!";

Event Listeners

  • Explanation: addEventListener() allows you to execute code in response to user actions like clicks.
document.getElementById("myButton").addEventListener("click", function() {
alert("Button clicked!");
});

 

Previous Article

Random Number Generator Explained: How Computers Pick Numbers (With Python, Java & Excel Examples)

Next Article

JavaScript for React Developers: 7 Must-Know Skills to Finally Understand React 🧠

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Subscribe to our Newsletter

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨