1๏ธโฃ What is JavaScript?
> JavaScript is a high-level, dynamic, untyped, and interpreted programming language primarily used for enhancing interactivity in web pages.
2๏ธโฃ Key Features of JavaScript
โข Interpreted Language: Runs directly in the browser without the need for compilation.
โข Event-driven: Responds to user actions such as clicks and keyboard input.
โข Prototype-based: Supports object-oriented programming through prototypes instead of classes.
3๏ธโฃ Data Types
โข Primitive Types:
โ
String: Represents text (e.g., "Hello World")โ
Number: Represents numeric values (e.g., 42, 3.14)โ
Boolean: Represents true or false values (true, false)โ
Undefined: A variable that has been declared but not assigned a value.โ
Null: Represents an intentional absence of any object value.โ
Symbol: A unique and immutable primitive value (ES6).โข Reference Types:
โ
Object: Collections of key-value pairs.โ
Array: An ordered list of values.โ
Function: A callable object.4๏ธโฃ Variables
โข Declaration Keywords:
โ
var: Function-scoped or globally scoped.โ
let: Block-scoped and can be reassigned.โ
const: Block-scoped and cannot be reassigned.5๏ธโฃ Control Structures
โข Conditional Statements:
if (condition) {
// code to execute if condition is true
} else {
// code to execute if condition is false
}
โข Switch Statement:
switch (expression) {
case value1:
// code block
break;
case value2:
// code block
break;
default:
// default code block
}
โข Loops:
โ For Loop:
for (let i = 0; i < array.length; i++) {
// code to execute
}
โข While Loop:
while (condition) {
// code to execute
}
6๏ธโฃ Functions
โข Function Declaration:
function functionName(parameters) {
// code to execute
return value;
}
โข Arrow Functions (ES6):
const functionName = (parameters) => {
// code to execute
return value;
};
7๏ธโฃ Objects and Arrays
โข Creating Objects:
const person = {
name: "John",
age: 30,
greet: function() {
console.log("Hello!");
}
};
โข Accessing Object Properties:
console.log(person.name); // Dot notation
console.log(person['age']); // Bracket notation
โข Arrays:
const fruits = ["apple", "banana", "cherry"];
console.log(fruits[0]); // Accessing first element
8๏ธโฃ ES6 Features
โข Template Literals:
const greeting = Hello, ${name}!;โข Destructuring Assignment:
const { name, age } = person;โข Spread Operator:
const newArray = [...oldArray, newItem];
9๏ธโฃ Asynchronous JavaScript
โข Callbacks:
function fetchData(callback) {
// Simulate async operation
setTimeout(() => {
callback("Data received");
}, 1000);
}โข Promises:
const promise = new Promise((resolve, reject) => {
// Async operation
if (success) {
resolve("Success!");
} else {
reject("Error!");
}
});โข Async/Await:
async function fetchData() {
try {
const result = await promise;
console.log(result);
} catch (error) {
console.error(error);
}
}๐ Best Practices
โข Use
let and const instead of var for better scope control.โข Keep your functions small and focused on a single task.
โข Use meaningful variable and function names.
โข Comment your code for clarity.
โข Avoid global variables to prevent conflicts.
1๏ธโฃ1๏ธโฃ Common JavaScript Libraries/Frameworks
โข jQuery: Simplifies DOM manipulation and event handling.
โข React: A library for building user interfaces.
โข Node.js: A runtime environment for executing JavaScript server-side.