JavaScript
JavaScript
md 2025-05-13
JavaScript
Why We Need JavaScript
1. Making Websites Interactive
2. Client-Side Scripting
3. Asynchronous Programming
Interacts with APIs like Google Maps, Geolocation, and Fetch API
Enables advanced features like live search and real-time updates
7. Cross-Browser Compatibility
JavaScript used in React Native (mobile) and Electron (desktop) for cross-platform apps
1 / 10
JavaScript.md 2025-05-13
React, Angular, Vue.js, Express.js, jQuery, and more for faster development
12. Universality
JavaScript is the only language supported across all browsers, both client and server-side
Used in web, mobile, desktop, and even IoT applications
What is JavaScript
Lightweight, interpreted programming language
Follows ECMAScript standard
Runs in browser and server (Node.js)
Event-driven and asynchronous
Environment Setup
Use browser console
Use code editors (VS Code, Sublime)
Node.js for backend JavaScript
Type of Identifiers
Variable Identifiers: Used for variables and constants.
2 / 10
JavaScript.md 2025-05-13
Property and Method Identifiers: Used to access properties and methods of objects.
Data Types
Primitive
String
Number
Boolean
Null
Undefined
Symbol
BigInt
Non-Primitive
Objects
Arrays
Functions
Date
Types of Statements
1. Non-Conditional Statements: Declare variables, express assignments, and return values.
2. Conditional Statements: Execute code based on conditions (e.g., if, switch).
3. Looping Statements: Repeat a block of code multiple times (e.g., for, while).
4. Exception Handling: Handle errors gracefully (e.g., try-catch).
5. Other Statements: Handle control flow (e.g., break, continue).
Non-Conditional Statements
Non-Conditional Statements in JavaScript are the basic building blocks of the program. They perform
actions without making any decisions or checking conditions.-
Declaration Statements: Declare variables and functions.
Expression Statements: Perform operations like assignments or function calls.
Return Statement: Returns a value from a function or exits early.
Break Statement: Exits a loop or switch statement prematurely.
3 / 10
JavaScript.md 2025-05-13
Continue Statement: Skips the current iteration of a loop and continues to the next one.-
Conditional Statements
if
else if
else
switch
Types of Loops
for
while
do...while
for...in - used for object iteration - not recommended for array elements
for...of - iterable objects like arrays, strings, and other iterable collections.
Types of Functions
1. Function Declaration: A named function defined using the function keyword.
2. Function Expression: A function without a name (anonymous), assigned to a variable.
3. Arrow Function: A concise syntax for functions, introduced in ES6, with lexical this.
4. Immediately Invoked Function Expression (IIFE): A function that is executed immediately after its
definition.
5. Constructor Function: A function used to create objects, typically used with the new keyword.
6. Generator Function: A function that can pause and resume its execution using the yield keyword.
7. Anonymous Function as a Parameter: A function without a name, often passed as an argument to
other functions (e.g., callbacks).
8. Recursive Function: A function that calls itself to solve a problem through repeated execution.
Arrow Function
Arrow functions provide a concise syntax for writing functions.
4 / 10
JavaScript.md 2025-05-13
They do not have their own this, arguments, super, or new.target, making them suitable for simple
functions and scenarios where you want to maintain the lexical context of this.
Arrow functions are perfect for callbacks and array methods, but they should not be used as
constructors.
Function Parameters
1. Default Parameters: Parameters can have default values.
2. Rest Parameters: Represent an indefinite number of parameters as an array.
3. Destructuring Parameters: Unpack values from objects or arrays directly in the function signature.
4. Named vs. Anonymous Parameters: Named parameters are explicitly defined, whereas anonymous
ones are often used in callback functions.
5. Arrow Functions: Parameters in arrow functions follow the same rules as regular functions but with a
more concise syntax.
6. Variable Numbers of Parameters: Use rest parameters (...) or the arguments object to handle an
unknown number of arguments.
Nested Function
Nested functions are functions defined inside other functions.
They can access variables and parameters from the outer function, which is useful for encapsulating
behavior and creating closures.
Closures allow inner functions to "remember" the environment in which they were created, providing
powerful capabilities like data privacy and function factories.
Built-in Functions
String Methods
st.charAt(index)
st.concat(s1,s2,...)
st.includes(search)
st.indexOf(search)
st.slice(startInd, endInd)
st.toUpperCase()
st.toLowerCase()
Array Methods
arr.push(ele)
arr.pop()
arr.shift() - remove first
arr.unshift(ele) - add at first
arr.concat(brr)
arr.forEach(callback)
arr.map(callback)
arr.filter(callback)
5 / 10
JavaScript.md 2025-05-13
Math Methods
Math.abs(x)
Math.pow(base, exp)
Math.round(x)
Math.floor(x)
Math.ceil(x)
Math.max(...x)
Math.min(...x)
JSON Methods
JSON.stringify(obj)
JSON.parse(jsonString)
class Person {
constructor(name) {
this.name = name;
}
}
6 / 10
JavaScript.md 2025-05-13
Inheritance: Subclasses can extend base classes using the extends keyword. The subclass can then use
the super() method to call the parent class's constructor.
Method Overriding: Subclasses can override methods from the parent class to provide more specific
behavior.
element.addEventListener("click", handler);
JavaScript has plain objects, arrays, functions, and other built-in objects.
User-defined
Built-in (Array, Date, Math)
Creating Objects
Object literals
new Object()
Constructor functions
Object.create()
Destructuring Objects
It allows you to extract properties from an object and assign them to variables with optional renaming
and default values.
Spread Operator
The spread operator is a powerful and concise tool in JavaScript that simplifies working with arrays and
objects. It allows you to:
7 / 10
JavaScript.md 2025-05-13
Arrays
Creating Arrays
Destructuring Arrays
Accessing Arrays
Indexing: arr[0]
Array Methods
Asynchronous Programming
Helps handle time-based and delayed operations
Prevents blocking
Callbacks
Function passed as an argument
setTimeout(() => {
console.log("Hello");
8 / 10
JavaScript.md 2025-05-13
}, 1000);
When callbacks are nested within callbacks, the code becomes messy and hard to maintain. This is
called callback hell.
doTask1(() => {
doTask2(() => {
doTask3(() => {
console.log("All tasks done!");
});
});
});
Promises
Promises solve callback hell.
fetch(url)
.then((res) => res.json())
.then((data) => console.log(data))
.catch((err) => console.error(err));
Promise States
9 / 10
JavaScript.md 2025-05-13
Fetch API
Used to make HTTP requests
fetch(url, {
method: "GET/POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(data),
});
Creating Module
Consuming Module
Connect
GitHub omteja04
LinkedIn omteja
10 / 10