sanskaarz / LearnJS

JavaScript Concepts
MIT License
7 stars 20 forks source link
codescene collaborate copilot deepscan frontend ghdesktop github github-campus-experts github-codespaces github-copilot github-pages gitkraken gitlens javascript student-vscode
# [LearnJS](https://github.com/sanskaarz/LearnJS)

JavaScript Learning Roadmap

Welcome to the JavaScript Learning Roadmap! This guide will help you master JavaScript, from the basics to advanced concepts. Follow the steps below to enhance your JavaScript skills.

Table of Contents

  1. Introduction to JavaScript
  2. JavaScript Basics
  3. Control Structures and Loops
  4. Functions and Scope
  5. Object-Oriented Programming (OOP)
  6. Asynchronous JavaScript
  7. JavaScript in the Browser
  8. Advanced JavaScript Concepts
  9. JavaScript Ecosystem and Tools
  10. Projects and Practice

Introduction to JavaScript

JavaScript Basics

Control Structures and Loops

Functions and Scope

Object-Oriented Programming (OOP)

Asynchronous JavaScript

JavaScript in the Browser

Advanced JavaScript Concepts

JavaScript Ecosystem and Tools

Projects and Practice


Feel free to modify and add more details to each section as you progress through your JavaScript journey. Happy coding!


Additional Resources


This roadmap gives a structured pathway to learning JavaScript, starting from the basics and advancing to more complex topics, with plenty of practice and project opportunities along the way.


# [Let's get started](https://github.com/sanskaarz/LearnJS)

Variables

Types of Scopes

  1. let
  2. var
  3. const
| | Block Scoped | Hoisting | Reassignment | Initialization | |-------|---------------|----------|--------------|----------------| | `let` | Yes | No | Yes | Optional | | `var` | No | Yes | Yes | Optional | | `const`| Yes | No | No | Required |

Primitive Data Types

  1. null
  2. number
  3. symbol
  4. string
  5. boolean
  6. bigint
  7. undefined

Examples:

let a = null;
let b = 345;
let c = true; // can also be false
let d = BigInt("567") + BigInt("3");
let e = "Harry";
let f = Symbol("I am a symbol");
let g;

Type of a Variable

console.log(typeof a); // Output: object

Objects

Objects in JavaScript are similar to dictionaries in Python (key-value pairs).

Accessing Object Values

  1. objectname.key
  2. objectname["Keyname"]

Operators

Arithmetic Operators

Operator Name Description Example Result
+ Addition Adds two operands. 5 + 3 8
- Subtraction Subtracts the second operand from the first. 5 - 3 2
* Multiplication Multiplies two operands. 5 * 3 15
/ Division Divides the first operand by the second. 6 / 3 2
% Modulus (Remainder) Returns the remainder when the first operand is divided by the second. 5 % 2 1
** Exponentiation Raises the first operand to the power of the second operand. 2 ** 3 8
++ Increment Increases an integer value by one. let a = 1; a++ 2
-- Decrement Decreases an integer value by one. let b = 2; b-- 1

Notes:

Assignment Operators


Operator Name Description Example Equivalent to Result
= Assignment Assigns a value to a variable. x = 5 x = 5
+= Addition Assignment Adds the right operand to the left operand and assigns the result to the left operand. x += 3 x = x + 3 x becomes 8
-= Subtraction Assignment Subtracts the right operand from the left operand and assigns the result to the left operand. x -= 2 x = x - 2 x becomes 6
*= Multiplication Assignment Multiplies the left operand by the right operand and assigns the result to the left operand. x *= 4 x = x * 4 x becomes 20
/= Division Assignment Divides the left operand by the right operand and assigns the result to the left operand. x /= 5 x = x / 5 x becomes 4
%= Modulus Assignment Takes the modulus using the two operands and assigns the result to the left operand. x %= 3 x = x % 3 x becomes 1
**= Exponentiation Assignment Raises the left operand to the power of the right operand and assigns the result to the left operand. x **= 2 x = x ** 2 x becomes 16

Notes:


Comparison Operators

Logical Operators

Comments in JavaScript

// Single line comment

/* 
   Multi-line comment 
*/

Conditional Statements

  1. if-else

    if (condition) {
       // code to be executed if condition is true
    } else {
       // code to be executed if condition is false
    }
  2. Nested if-else

    if (condition1) {
       // code to be executed if condition1 is true
    } else if (condition2) {
       // code to be executed if condition2 is true
    } else {
       // code to be executed if both conditions are false
    }
  3. Ternary Operator

    condition ? expression1 : expression2;
  4. Switch Case

    switch (variable) {
       case "1":
           // code to be executed if variable is "1"
           break;
       case "2":
           // code to be executed if variable is "2"
           break;
       default:
           // code to be executed if variable doesn't match any case
    }

Output Methods

  1. prompt("output"): Displays a dialog box with a message and an input field for the user.
  2. alert("output"): Displays an alert box with a message and an OK button.
  3. console.log("output"): Prints a message to the browser console for debugging.
  4. console.error("error message"): Prints an error message to the browser console.
  5. console.warn("warning message"): Prints a warning message to the browser console.
  6. document.write("output"): Writes content directly into the HTML document.
  7. document.getElementById("demo").innerHTML = "output": Sets the content of an HTML element with the specified id.

TypeCasting

Typecasting is converting a value from one data type to another. It can be implicit or explicit.

Examples:

let a = "123";
a = Number.parseInt(a); // Convert string to a number
a = parseInt(a); // Convert string to a number

Loops

1. for

for (initialize; condition; increment) {
  // code to be executed
}

2. for-in

Iterates over the properties of an object.

const dict = {
  water: "pani",
  lens: "chasma",
  bag: "basta",
  mobile: "phone"
};

for (let key in dict) {
  console.log(key); // prints keys
  console.log(dict[key]); // prints values
}

3. for-of

Iterates over the values of an iterable object.

let name = "sanskar";
for (let char of name) {
  console.log(char); // prints each character
}

4. while

while (condition) {
  // code to be executed
}

5. do-while

do {
  // code to be executed
} while (condition);

Functions

Reusable blocks of code that perform specific tasks.

Function without Parameters

function funcName() {
  // code to be executed
}
funcName(); // calling the function

Function with Parameters

function myFunc(a, b) {
  return a + b;
}

Arrow Function

// Before Arrow Function
funcName = function() {
  return "Hello World!";
}

// After Arrow Function
funcName = () => "Hello World!";

Strings

  1. "Hello my value is " + a // double quotes
  2. 'Hello my value is ' + a // single quotes
  3. Hello my value is ${a} // backticks (template literals)

Escape Sequences

String Methods

Example:

let strName = "Hello World";
console.log(strName.charAt(4)); // Output: "o"
console.log(strName.charCodeAt(6)); // Output: 87
console.log(String.fromCharCode(72)); // Output: "H"
console.log(strName.endsWith("World")); // Output: true
console.log(strName.includes("lo")); // Output: true
console.log(strName.indexOf("o")); // Output: 4
console.log(strName.lastIndexOf("o")); // Output: 7
console.log(strName.length); // Output: 11
console.log(strName.localeCompare("Hello")); // Output: 1
console.log(strName.match(/World/)); // Output: ["World"]

Splitting a String into an Array

let text = "a,b,c,d,e,f";
let myArray = text.split(",");
console.log(myArray[2]); // Output: "c"

Arrays

Array Methods

  1. push()

    • Adds one or more elements to the end of an array and returns the new length.
      let arr = [1, 2, 3];
      arr.push(4);
      console.log(arr); // [1, 2, 3, 4]
  2. pop()

    • Removes the last element from an array and returns that element.
      let arr = [1, 2, 3, 4];
      arr.pop();
      console.log(arr); // [1, 2, 3]
  3. shift()

    • Removes the first element from an array and returns that element.
      let arr = [1, 2, 3, 4];
      arr.shift();
      console.log(arr); // [2, 3, 4]
  4. unshift()

    • Adds one or more elements to the beginning of an array and returns the new length

.

   let arr = [1, 2, 3, 4];
   arr.unshift(0);
   console.log(arr); // [0, 1, 2, 3, 4]
  1. splice()

    • Adds/removes elements from an array.
      let arr = [1, 2, 3, 4];
      arr.splice(2, 1, 10);
      console.log(arr); // [1, 2, 10, 4]
  2. slice()

    • Returns a shallow copy of a portion of an array.
      let arr = [1, 2, 3, 4];
      let newArr = arr.slice(1, 3);
      console.log(newArr); // [2, 3]
  3. concat()

    • Merges two or more arrays.
      let arr1 = [1, 2];
      let arr2 = [3, 4];
      let newArr = arr1.concat(arr2);
      console.log(newArr); // [1, 2, 3, 4]
  4. join()

    • Joins all elements of an array into a string.
      let arr = [1, 2, 3, 4];
      let str = arr.join("-");
      console.log(str); // "1-2-3-4"
  5. map()

    • Creates a new array with the results of calling a provided function on every element.
      let arr = [1, 2, 3, 4];
      let newArr = arr.map(x => x * 2);
      console.log(newArr); // [2, 4, 6, 8]
  6. filter()

    • Creates a new array with all elements that pass the test implemented by the provided function.
      let arr = [1, 2, 3, 4];
      let newArr = arr.filter(x => x > 2);
      console.log(newArr); // [3, 4]
  7. reduce()

    • Executes a reducer function on each element, resulting in a single output value.
      let arr = [1, 2, 3, 4];
      let sum = arr.reduce((acc, curr) => acc + curr, 0);
      console.log(sum); // 10
  8. sort()

    • Sorts the elements of an array.
      let arr = [4, 2, 3, 1];
      arr.sort();
      console.log(arr); // [1, 2, 3, 4]
  9. reverse()

    • Reverses the elements in an array.
      let arr = [1, 2, 3, 4];
      arr.reverse();
      console.log(arr); // [4, 3, 2, 1]
  10. find()

    • Returns the value of the first element that satisfies the provided testing function.
      let arr = [1, 2, 3, 4];
      let found = arr.find(x => x > 2);
      console.log(found); // 3
  11. findIndex()

    • Returns the index of the first element that satisfies the provided testing function.
      let arr = [1, 2, 3, 4];
      let index = arr.findIndex(x => x > 2);
      console.log(index); // 2

Window Object

The window object represents the browser's window. It contains various properties and methods.

Common Methods

Timer Methods

Example:

setTimeout(() => {
  console.log("Hello after 3 seconds");
}, 3000);

let interval = setInterval(() => {
  console.log("Repeating every 2 seconds");
}, 2000);

// To clear the interval
clearInterval(interval);

DOM Manipulation

The Document Object Model (DOM) allows scripts to update the content, structure, and style of a document.

Selecting Elements

Modifying Elements

Creating and Removing Elements

Event Handling

Example:

let button = document.getElementById("myButton");
button.addEventListener("click", () => {
  alert("Button clicked!");
});

let div = document.createElement("div");
div.innerHTML = "Hello, World!";
document.body.appendChild(div);

document.body.removeChild(div);

Adding External JavaScript

Add the following line in your HTML file inside the <head> or <body> tag:

<script src="https://github.com/sanskaarz/LearnJS/raw/main/filename.js"></script>

This concludes the LearnJS summary of fundamental JavaScript concepts and syntax.