Skip to content

Latest commit

 

History

History
176 lines (132 loc) · 3.52 KB

File metadata and controls

176 lines (132 loc) · 3.52 KB

Summary of JavaScript Documentation

Overview

JavaScript is a lightweight, interpreted programming language primarily used for creating dynamic and interactive web applications. It supports object-oriented, procedural, and functional programming styles.


Key Features

  • Dynamic Typing: Variables can hold different data types at runtime.
  • First-Class Functions: Functions are treated as objects and can be assigned to variables, passed as arguments, or returned by other functions.
  • Event-Driven: Executes code in response to user actions or system events.
  • Prototypal Inheritance: Objects can inherit properties and methods from other objects.

Core Concepts

Variables

  • Declaration:
    • var: Function-scoped (legacy).
    • let: Block-scoped.
    • const: Block-scoped, immutable reference.
  • Example:
    let name = "JavaScript";
    const version = "ES6";

Data Types

  • Primitive: string, number, boolean, null, undefined, bigint, symbol
  • Reference: object, array, function

Operators

  • Arithmetic: +, -, *, /, %
  • Comparison: ==, ===, !=, !==, <, >
  • Logical: &&, ||, !
  • Assignment: =, +=, -= etc.

Control Flow

Conditionals

  • Use if, else if, else, and switch for decision-making:
    if (age > 18) {
      console.log("Adult");
    } else {
      console.log("Minor");
    }

Loops

  • For Loop:
    for (let i = 0; i < 5; i++) {
      console.log(i);
    }
  • While Loop:
    let i = 0;
    while (i < 5) {
      console.log(i);
      i++;
    }

Functions

  • Declaration:
    function greet(name) {
      return `Hello, ${name}`;
    }
  • Arrow Functions:
    const greet = (name) => `Hello, ${name}`;

Objects and Arrays

Objects

  • Key-value pairs:
    const person = { name: "Alice", age: 25 };

Arrays

  • Ordered collections:
    const numbers = [1, 2, 3];
    numbers.push(4);

DOM Manipulation

  • Selecting Elements:
    const element = document.getElementById("myElement");
  • Event Handling:
    element.addEventListener("click", () => {
      console.log("Element clicked!");
    });

Asynchronous JavaScript

  • Promises:
    fetch("https://api.example.com")
      .then((response) => response.json())
      .then((data) => console.log(data))
      .catch((error) => console.error(error));
  • Async/Await:
    async function fetchData() {
      const response = await fetch("https://api.example.com");
      const data = await response.json();
      console.log(data);
    }

Error Handling

  • Use try, catch, and finally for error handling:
    try {
      const data = JSON.parse("{invalid json}");
    } catch (error) {
      console.error("Parsing error:", error);
    } finally {
      console.log("Done");
    }

Best Practices

  • Use const and let over var.
  • Avoid polluting the global namespace.
  • Use strict equality (===) to avoid type coercion issues.
  • Modularize code using ES6 modules (import/export).

Resources

JavaScript is a versatile language used in a wide range of applications, from web development to server-side programming and beyond.