JavaScript Introduction
JavaScript is a programming language used to add logic, interactivity, and dynamic behavior to web
pages. It runs in the browser and can also run on servers using [Link].
Why JavaScript is Needed
HTML gives structure, CSS gives style, JavaScript gives logic. Without JavaScript, web pages
cannot respond to user actions like clicks or input.
Variables
Variables are used to store data.
var (old), let (changeable), const (fixed).
Example:
let age = 20;
const pi = 3.14;
Data Types
Primitive: Number, String, Boolean, Undefined, Null, BigInt, Symbol.
Non-Primitive: Object, Array, Function.
Example:
let name = 'Manas'; let isStudent = true;
Type Checking
Use typeof operator to check data type.
Example: typeof 10 → 'number'
Objects
Objects store data in key-value pairs.
Example:
let student = { name: 'Rahul', age: 21, passed: true };
Accessing Object Data
Dot notation: [Link]
Bracket notation: student['age']
Conditions (if-else)
Used to make decisions based on conditions.
Example:
if(age >= 18) { [Link]('Adult'); } else { [Link]('Minor'); }
Comparison Operators
==, ===, !=, !==, >, <, >=, <=.
Always prefer === for strict comparison.
Logical Operators
AND (&&), OR (||), NOT (!).
Used to combine multiple conditions.
Loops
Loops repeat code automatically.
Types: for, while, do-while.
For Loop
Example:
for(let i = 1; i <= 5; i++) { [Link](i); }
While Loop
Example:
let i = 1; while(i <= 5) { [Link](i); i++; }
Do-While Loop
Executes at least once.
Example:
let i = 1; do { [Link](i); i++; } while(i <= 5);
Functions
Functions are reusable blocks of code.
Example:
function add(a, b) { return a + b; }
Function Call
Example:
let sum = add(2, 3);
Arrow Functions
Shorter syntax for functions.
Example:
const square = x => x * x;
Parameters vs Arguments
Parameters are variables in function definition.
Arguments are values passed to the function.
Return Statement
Used to send value back from function.
Code after return does not execute.
Common Mistakes
Using == instead of ===, forgetting loop increment, not returning value from function, modifying
const variables.
Best Practices
Use let and const, meaningful variable names, indent code properly, avoid global variables.
End Note
Revise this PDF regularly and practice writing code daily to master JavaScript basics.