JavaScript Noted
JavaScript Noted
intermediate learners. We'll break it down into several key sections: Basics, Data Types,
Control Flow, Functions, Objects, DOM Manipulation, ES6+ Features, and Asynchronous
JavaScript.
1. Basics
What is JavaScript?
Embedding JavaScript
Comments
2. Data Types
Primitive Types
1. String: "Hello"
2. Number: 42, 3.14
3. Boolean: true, false
4. Undefined: A variable declared but not assigned.
5. Null: Intentional absence of value.
6. Symbol: Unique and immutable value (rarely used).
7. BigInt: For integers larger than 2^53 - 1.
Non-Primitive Types
Type Checking
typeof operator:
console.log(typeof "Hello"); // string
console.log(typeof 42); // number
3. Variables
Declaration Keywords
Examples
var a = 10;
let b = 20;
const PI = 3.14;
4. Operators
Arithmetic Operators
+, -, *, /, % (modulus), ** (exponentiation).
Comparison Operators
== (loose equality), === (strict equality), !=, !==, >, <, >=, <=.
Logical Operators
Assignment Operators
if-else
if (x > 10) {
console.log("Greater");
} else {
console.log("Smaller or equal");
}
switch-case
switch (day) {
case 1:
console.log("Monday");
break;
default:
console.log("Other day");
}
Loops
for
for (let i = 0; i < 5; i++) {
console.log(i);
}
while
let i = 0;
while (i < 5) {
console.log(i);
i++;
}
do-while
let j = 0;
do {
console.log(j);
j++;
} while (j < 5);
6. Functions
Function Declaration
function add(a, b) {
return a + b;
}
Function Expression
const multiply = function(a, b) {
return a * b;
};
Arrow Functions
const divide = (a, b) => a / b;
Default Parameters
function greet(name = "Guest") {
console.log("Hello, " + name);
}
7. Objects
Creating Objects
const person = {
name: "Alice",
age: 25,
greet() {
console.log("Hello, " + this.name);
},
};
Accessing Properties
8. Arrays
Creating Arrays
const fruits = ["Apple", "Banana", "Mango"];
Array Methods
Modifying Content
title.textContent = "New Title";
Event Handling
button.addEventListener("click", () => {
alert("Button clicked!");
});
Destructuring
const [a, b] = [1, 2];
const { name, age } = person;
function sum(...numbers) {
return numbers.reduce((acc, num) => acc + num);
}
Classes
class Animal {
constructor(name) {
this.name = name;
}
speak() {
console.log(`${this.name} makes a sound.`);
}
}
Async/Await
async function fetchData() {
const response = await fetch("https://api.example.com");
const data = await response.json();
console.log(data);
}
Summary
JavaScript is versatile with a range of features for building dynamic
web pages.
Understanding the basics, data types, and control flow is essential.
Mastering functions, objects, and asynchronous programming
unlocks advanced capabilities.
ES6+ features streamline code and enhance readability.