O Level JavaScript Notes
O Level JavaScript Notes
(INTRODUCTION + HISTORY)
What is JavaScript ?
JavaScript is a scripting language used to make web pages interactive and
dynamic. It allows a page to react when a user clicks, types or moves the
mouse.
What is ECMAScript?
It is the official standard for JavaScript.
JavaScript Engines
Browsers run JavaScript using engines.
Browser Engine
Chrome V8
Firefox SpiderMonkey
Edge Chakra
Safari JavaScriptCore
MCQ Tip:
Chrome uses V8 engine.
File Information
JavaScript file extension: .js
Script tag: <script>
JavaScript code can be written inside HTML or in an external file.
Features of JavaScript
High-level
Interpreted
Event-driven
Object-based (not fully object-oriented)
Loosely typed (no need to declare data type)
MCQ Tip:
JavaScript is object-based, not object-oriented.
Example
<script>
alert("JavaScript Working");
</script>
Inline JavaScript
Inline JavaScript is written directly inside an HTML tag using an event
attribute.
Example:
<button >
In Head In Body
<head> <body>
<script> <script>
alert("Head script"); alert("Body script");
</script> </script>
</head> </body>
External JavaScript
External JavaScript is written in a separate .js file and linked to the HTML
page.
HTML File ([Link]):
<script src="[Link]"></script>
JavaScript File ([Link]):
alert("This is external JavaScript");
Wrong:
<script src="[Link]">
alert("Hello"); // Not allowed
</script>
Example
[Link] ("Hello Students");
[Link] ()
It prints output inside the browser’s console.
Used mainly for debugging and checking values.
Syntax
[Link] ("message");
Example
[Link] ("This is a test");
alert ()
A popup box that displays a simple message to the user.
Why it is used
To show warnings
To display information
To notify the user about something important
Syntax
alert ("message");
Example
alert ("Welcome to JavaScript!");
Output
A popup appears with the message:
Welcome to JavaScript!
confirm()
A popup box that asks the user to choose OK or Cancel.
Why it is used
To confirm actions
For yes/no decisions
Before deleting data or submitting a form
What it returns
true → if user clicks OK
false → if user clicks Cancel
Syntax
confirm("Your message");
Example
var ans = confirm("Do you want to continue?");
if (ans) {
[Link]("You clicked OK");
} else {
[Link]("You clicked Cancel");
}
Output
Case 1: User clicks OK
Popup shows:
Do you want to continue?
User selects OK
Result on webpage:
You clicked OK
Popup shows:
Do you want to continue?
Result on webpage:
prompt ()
A popup box that asks the user to enter some text.
Why it is used
To collect information from the user
To take input like name, age, number, etc.
What it returns
The text typed by the user (as a string)
null if the user clicks Cancel
Syntax
prompt("Your message");
Example
var name = prompt("Enter your name:");
[Link]("Hello " + name);
Output
If the user enters: Aditya
The webpage shows:
Hello Aditya
(Variables In JavaScript)
A variable is a named container used to store data and it holds values like
numbers, text, or results.
Example
var name = "Ramesh";
Example:
var age = 20;
Important Points:
Has function scope
Can be redeclared
Can be updated
var is not block-scoped
let
let is modern and safer than var.
Example:
let city = "Delhi";
Important Points:
Has block scope { }
Cannot be redeclared
Can be updated
const
const is used for fixed values.
Example:
const pi = 3.14;
Important Points:
Block-scoped
Cannot be redeclared
Cannot be updated
But objects and arrays inside const can change
Must have a value at the time of declaration
Difference Table
Feature var let const
Scope Function Block Block
Redeclare Yes No No
Update (Reassign) Yes Yes No
Must assign value? No No Yes
Introduced in Old JS ES6 ES6
Arithmetic Operators
Operator Meaning Example Output
+ Add 5+3 8
- Subtract 5-3 2
* Multiply 5*3 15
/ Divide 6/2 3
% Remainder 7%3 1
** Power 2 ** 3 8
Assignment Operators
Operator Meaning Example Result
= Assign x=5 5
+= Add and assign x += 2 x=x+2
-= Subtract and assign x -= 2 x=x-2
*= Multiply and assign x *= 2 x=x*2
/= Divide and assign x /= 2 x=x/2
%= Remainder and assign x %= 2 x=x%2
Comparison Operators
Operator Meaning Example Output
== Equal (value) 5 == "5" true
=== Strict equal (value + type) 5 === "5" false
!= Not equal (value) 5 != "5" false
!== Strict not equal 5 !== "5" true
> Greater than 6>5 true
< Lesser than 4<5 true
>= Greater or equal 5 >= 5 true
<= Lesser or equal 4 <= 5 true
Logical Operators
Operator Meaning Example Output
&& Logical AND true && false false
|| Logical OR true && false true
! Logical NOT !true false
Example
var x = 5;
[Link](x++); // output: 5
[Link](x); // output: 6
String Operator
In JavaScript, + is used to join strings.
Example:
"Hello" + " World"
Output:
Hello World
MCQ Trap
2 + "2" = "22" (String concatenation)
Unary Operators
Operator Meaning Example
Typeof Shows data type typeof "hello"
delete Removes property from object delete [Link]
Ternary Operator
Syntax:
condition ? valueIfTrue : valueIfFalse;
Example:
var age = 18;
var vote = (age >= 18) ? "Yes" : "No";
(Conditional Statements)
Conditional statements allow JavaScript to make decisions based on conditions.
JavaScript has three main conditional statements:
1. if
2. if…else
3. else if
4. switch
if Statement
Runs a block of code only if the condition is true.
Syntax
if (condition) {
statement;
}
Example
if (5 > 3) {
[Link]("Yes");
}
Output: Yes
if…else Statement
Runs one block when condition is true, otherwise another block.
Syntax
if (condition) {
statement1;
} else {
statement2;
}
Example
var age = 15;
if (age >= 18) {
[Link] ("Adult");
} else {
[Link] ("Minor");
}
Output: Minor
else if Statement
Checks multiple conditions.
Syntax
if (condition1) {
code1;
} else if (condition2) {
code2;
} else {
code3;
}
Example
var marks = 75;
switch Statement
Used when you have many specific choices.
Syntax
switch (expression) {
case value1:
statement;
break;
case value2:
statement;
break;
default:
statement;
}
Example
var day = 2;
switch (day) {
case 1: [Link]("Monday"); break;
case 2: [Link]("Tuesday"); break;
case 3: [Link]("Wednesday"); break;
default: [Link]("Unknown");
}
Output: Tuesday
Important Notes for Exams
if only checks one condition.
else has no condition.
else if can repeat many times.
switch uses === internally (strict match).
break stops execution inside a switch.
Without break, next cases also run (called fall-through).
(Loops in JavaScript)
Loops run a block of code again and again until a condition becomes false.
JavaScript has 3 main loops:
1. for loop
2. while loop
3. do…while loop
for Loop
Used when you know how many times the loop should run.
Syntax
for (initialization; condition; increment) {
// code here;
}
Example
for (var i = 1; i <= 5; i++) {
[Link](i);
}
Output
1
2
3
4
5
Important points
Runs from start to end.
Stops when condition becomes false.
while Loop
Used when you do not know how many times the loop will run.
Syntax
while (condition) {
// code here;
}
Example
var i = 1;
while (i <= 3) {
[Link](i);
i++;
}
Output
1
2
3
Important point
Condition is checked first.
If condition is false at the start → loop never runs.
do…while Loop
This loop always runs at least once.
Syntax
do {
code;
} while (condition);
Example
let i = 1;
do {
[Link](i);
i++;
} while (i <= 3);
Output
1
2
3
Important point
Code runs first, condition checked later.
break Statement
Used to stop the loop immediately.
Example:
for (var i = 1; i <= 5; i++) {
if (i == 3) break;
[Link](i);
}
Output:
1
2
continue Statement
Skips the current loop iteration and moves to the next one.
Example:
for (var i = 1; i <= 5; i++) {
if (i == 3) continue;
[Link](i);
}
Output:
1
2
4
5
Which loop uses all three parts (init, condition, for loop
increment)?
(Functions in JavaScript)
A function is a named block of code that performs a task. It runs only
when you call it.
Use functions to reuse code and keep programs organized.
One-line: Function = reusable piece of code.
Example
function hello() {
[Link]("Hello");
}
Function Definition
This means “creating” the function.
Syntax
function functionName() {
// code here
}
Example
function greet() {
[Link]("Good Morning");
}
Function Calling
This means running the function.
Syntax
functionName();
Example
greet();
Output:
Good Morning
Output: 10
Syntax
function functionName(parameters) {
return value;
}
Example
function add(a, b) {
return a + b;
}
Output: 8
Explanation:
return a + b; sends the answer back.
[Link](message());
(Arrays in JavaScript)
An array is a collection of multiple values stored in a single variable.
Example:
var fruits = ["Apple", "Banana", "Orange"];
Example
var numbers = [10, 20, 30];
Array Index
Index always starts from 0.
Value Index
Apple 0
Banana 1
Orange 2
Example:
var fruits = ["Apple", "Banana", "Orange"];
[Link](fruits[1]);
Output: Banana
Array Length
Number of elements in array.
var size = [Link];
[Link](size); // 3
What is DOM?
DOM is a structure that allows JavaScript to read, change, add, or remove
HTML elements.
Example uses:
Change text
Change CSS
Update values
Hide or show elements
onclick Event
Triggered when the user clicks an element.
Syntax
<button Me</button>
Example
<button Me</button>
<p id="demo"></p>
<script>
// Only changes <p> when button is clicked
function showMessage() {
[Link] ("demo").innerHTML = "Button Clicked!";
}
NOTE: ‘this’ keyword is used to refer current element within JavaScript event.
onchange Event
Triggered when the value of an input field changes.
Syntax
<input type="text" >Example
<input type="text" id="name" ><p id="demo"></p>
<script>
function displayName() {
var x = [Link]("name").value;
[Link] ("demo").innerHTML = "You entered: " + x;
}
</script>
Output:
After typing something in the box and leaving the field → shows: You entered: [value]
function mouseOut() {
[Link]("demo").innerHTML = "Mouse left!";
}
</script>
Step-by-Step Output
1. Page loads → Text: Move your mouse here
2. Move mouse over the paragraph → Text changes → Mouse is over!
3. Move mouse out → Text changes → Mouse left!
Example
<input type="text" id="inputBox" ><p id="demo"></p>
<script>
function keyDown() {
[Link]("demo").innerHTML = "Key is pressed!";
}
function keyUp() {
var x = [Link]("inputBox").value;
[Link]("demo").innerHTML = "You typed: " + x;
}
</script>
Step-by-Step Output
1. Page loads → <p> is empty.
2. Press a key → Text changes → Key is pressed! (onkeydown triggers)
3. Release the key → Text updates → You typed: [current input] (onkeyup triggers)
HTML:
<p id="msg">Hello</p>
JavaScript:
[Link] ("msg").innerHTML = "Welcome!” ;
Output: Welcome!
getElementsByClassName
Select multiple elements (by class).
[Link] ("myClass")[0].innerHTML = "Changed";
getElementsByTagName
Select elements using tag like p, h1, div.
[Link] ("p")[0].innerHTML = "Updated";
querySelector
Selects the first matching element.
[Link]("#msg");
[Link](".box");
[Link]("h1");
querySelectorAll
Selects all matching elements.
[Link] ("p")[1].innerHTML = "Hello";
JavaScript:
function changeText() {
[Link]("msg").innerHTML = "New Text";
}
Step-by-Step Behavior
1. Page loads → <p> shows: Hello World
2. Click the button → JavaScript runs
3. <p> content changes → New Text
Output
Before click: Hello World
After click: New Text
JavaScript:
[Link]("pic").src = "[Link]";
JavaScript:
[Link]("name").value = "Aditya";
[Link]("box").[Link] = "none";
Show
[Link]("box").[Link] = "block";
isNaN ()
isNaN() is a JavaScript function used to check whether a value is “Not a
Number”.
Returns true → if the value is not a number
Returns false → if the value is a number
Syntax
isNaN(value) // value → the variable or value you want to check
Examples
var x = 10;
[Link](isNaN(x)); // false, because 10 is a number
var y = "Hello";
[Link](isNaN(y)); // true, because "Hello" is not a number
var z = "123";
[Link](isNaN(z)); // false, because "123" can be converted to number
var a = "12abc"
[Link] (isNaN(a)); // true
var c = "";
[Link] (isNaN(c)); // false (empty string converts to 0)