[Go to site: main page, start]

0% found this document useful (0 votes)
154 views44 pages

O Level JavaScript Notes

JavaScript is a client-side scripting language created by Brendan Eich in 1995 for Netscape, designed to make web pages interactive and dynamic. It allows manipulation of HTML and CSS, validation of forms, and handling of events, and can also run on servers using Node.js. Key features include being case-sensitive, interpreted, and having a variety of data types and operators, with the ability to declare variables using var, let, or const.

Uploaded by

dohov15890
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
154 views44 pages

O Level JavaScript Notes

JavaScript is a client-side scripting language created by Brendan Eich in 1995 for Netscape, designed to make web pages interactive and dynamic. It allows manipulation of HTML and CSS, validation of forms, and handling of events, and can also run on servers using Node.js. Key features include being case-sensitive, interpreted, and having a variety of data types and operators, with the ability to declare variables using var, let, or const.

Uploaded by

dohov15890
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

(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.

Quick MCQ Facts


 JavaScript is a client-side scripting language.
 It is also used on servers through [Link].
 It is case-sensitive.
 It is interpreted, not compiled.

 Why JavaScript is Used


JavaScript is used to:
 Change HTML content
 Change CSS styles
 Validate form input
 Show messages (alert, prompt, confirm)
 Create animations
 Make simple games
 Handle events (click, keydown, mouseover)
 Build interactive pages
 History of JavaScript
 Who created JavaScript?
Brendan Eich

 When was JavaScript created?


1995

 Where was JavaScript created?


Netscape Communications (for the Netscape Navigator browser)

 Why was JavaScript created?


To make web pages interactive and dynamic, because earlier pages were fully
static.

 What was JavaScript’s original name?


 First name: Mocha
 Second name: LiveScript
 Final name: JavaScript

 What is ECMAScript?
It is the official standard for JavaScript.

 Who maintains ECMAScript?


ECMA International

 Important ECMAScript Versions


 ES5 (2009) → widely supported
 ES6 (2015) → modern JavaScript begins
Includes let, const, arrow functions, classes, template literals
MCQ Tip:
Original name → Mocha
ECMA → European Computer Manufacturers Association

 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.

 How JavaScript Runs


JavaScript runs inside a browser’s engine.
It reads code from top to bottom.

Example
<script>
alert("JavaScript Working");
</script>

 Super Quick MCQ Revision Table


Question Correct Answer
Who created JavaScript? Brendan Eich
When was it created? 1995
Where was it created? Netscape
Original name? Mocha
What is ECMAScript? JavaScript standard
Who maintains ECMAScript? ECMA International
JavaScript file extension? .js
JavaScript is case-sensitive? Yes
Type of language? Client-side scripting
JavaScript engine in Chrome? V8
Java and JavaScript same? No
JavaScript is interpreted? Yes
JavaScript can run on server? Yes, using [Link]
JavaScript comments? // and /* */

(How to add JavaScript to a webpage)


JavaScript can be added to a webpage in three main ways:
1. Inline Script
2. Internal Script
3. External Script

 Inline JavaScript
Inline JavaScript is written directly inside an HTML tag using an event
attribute.
Example:

<button >

Common Inline Events


 onclick
 onmouseover
 onmouseout
 onkeyup
 onchange
 Internal JavaScript
Internal JavaScript is written inside the <script> tag inside the HTML file.
Example:
<script>
[Link]("Internal Script Running");
</script>
Where to place the script?
 Inside <head> → Script loads before page content
 At end of <body> → Page loads first, script runs later (recommended)

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>

 Super Quick MCQ Revision Table


Question Correct Answer
How many ways to add JavaScript? 3
Inline script uses which attribute type? Event attributes like
onclick
Where do we place internal scripts? Head or body section
Recommended location for scripts? End of body
Extension of JavaScript file? .js
Tag used to add JS? <script>
How to link external JS? <script src="">
JavaScript runs when browser reaches <script> tag
which tag?
Can we write JS inside <script src="">? No
Inline JavaScript is used for? Small tasks

(Output Methods in JavaScript)


 [Link] ()
It prints text directly on the webpage.
It is used for basic output in simple programs.
Syntax
[Link] ("message");

Example
[Link] ("Hello Students");

Output on webpage: 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");

Where to see output:


Right click → Inspect → Console

(Popup Boxes in JavaScript)


JavaScript has three main popup boxes used to show messages, take input, or
ask for confirmation.

 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

 Case 2: User clicks Cancel

Popup shows:
Do you want to continue?

User selects Cancel

Result on webpage:

You clicked Cancel

 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";

 Three Ways to Declare Variables


JavaScript provides:
1. var
2. let
3. const
 var
var is the oldest way to create variables.

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

 Rules for Naming Variables


These always come in MCQs:
 Must start with letter, _, or $
 Cannot start with a number
 Cannot use spaces and special characters
 Case-sensitive (name ≠ Name)
 Cannot use reserved keywords
(for, var, break, while, return etc.)
Valid Names: Invalid Names:
 name  2name
 my_name  first name
 $price  for
 age2  @name

 Super Quick MCQ Revision Table


Question Answer
Which keyword is block-scoped? let, const
Which keyword can be redeclared? var
Which variables cannot change value? const
Which keyword is oldest? var
Variables are case-sensitive? Yes
Can a variable start with a number? No
Default value of unassigned variable? undefined
typeof null gives? object
let was introduced in? ES6
const must be initialized? Yes

(Data Types in JavaScript)


Data types tell what king of value a variable can store.
Type Example typeof
Number 10, 3.14, NaN, Infinity "number"
String "Hello", "123" "string"
Boolean true, false "boolean"
Undefined let x; "undefined"
Null let x = null; "object"
Object {name: "Amit"} "object"
Array [1,2,3] "object"
Function function(){} "function"

 Super Quick MCQ Revision Table


Question Correct Answer
What is the default value of an uninitialized undefined
variable?
What is the result of typeof null? "object"
What is the data type of [1, 2, 3]? object
What is the data type of NaN? number
JavaScript is what type of language? Dynamically typed
What is the output of "5" + 5? "55"
What is the output of "5" - 1? 4
Which operator checks both value and type? ===
What is the data type of a function? function
What is the result of typeof undefined? "undefined"
(Operators in JavaScript)
JavaScript operators are symbols used to perform actions on values.

 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

 Increment & Decrement Operators


Type Example Meaning
Pre-increment ++x Increase first, then use
Post-increment x++ Use first, then increase
Pre-decrement --x Decrease first
Post-decrement x-- Use first

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";

 Super Quick MCQ Revision Table


Question Answer
"5" == 5 true
"5" === 5 false
typeof null object
true && false false
"3" + 2 "32"
"3" - 2 1
5 > 2 && 2 > 1 true
!0 true
let x = 5; [Link](x++); 5
let x = 5; [Link](++x); 6
2 ** 3 8
7%4 3
"hello" + 5 "hello5"
`0
1 && "B" "B"

(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;

if (marks >= 90) {


[Link]("A");
} else if (marks >= 60) {
[Link]("B");
} else {
[Link] ("C");
}
Output: B

 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).

 Super Quick MCQ Revision Table


MCQ Question Answer
Which keyword checks a condition? if
Which keyword runs when all conditions fail? else
Which statement checks multiple conditions? else if
switch uses which comparison? strict (===)
What happens if break is removed in switch? next case runs
Output of if (0) {} nothing (0 is false)
Output of if ("hello") {} true ("hello" is truth)
Truth value of empty string "" false
Truth value of " " (space) true
Which statement has a default option? switch

(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

 Super Quick MCQ Revision Table


MCQ Question Correct Answer
Which loop runs at least once? do…while
Condition of while loop is checked at? Beginning
Condition of do…while loop is checked at? End
Which loop is best when number of iterations is for loop
known?
What stops a loop immediately? break
What skips one iteration? continue
Output: for (let i=1;i<3;i++) [Link](i); 1, 2
Output: while(false){[Link](1)} Nothing
Output: do{[Link](1)}while(false); 1
What is infinite loop? Loop never ends
Which loop can easily become infinite? while
Output: for(var i=0;i<3;i++){ if(i==1) break; 0
[Link](i);}
Output: for(var i=0;i<3;i++){ if(i==1) continue; 0, 2
[Link](i);}
What keyword controls loop flow? break, continue

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");
}

 Why Use Functions


 To reuse code
 To make code cleaner
 To perform tasks again and again
 To divide program into parts

 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

 Function without Parameters


Example
function show() {
return "Hi";
}
[Link](show());

 Function with Parameters


Parameters act like variables inside a function.
Syntax
function name(param1, param2) {
// code here
}
Example
function add(a, b) {
[Link](a + b);
}
add(4, 6);

Output: 10

 What is Function Arguments?


Arguments are the real values you pass while calling the function.
Example:
 a and b = parameters (formal argument)
 4 and 6 = arguments (actual argument)

 Function with Return


A return statement sends a value back from the function to the place where
it was called.
You use return when you want the function to give an output.

Syntax
function functionName(parameters) {
return value;
}

Example
function add(a, b) {
return a + b;
}

var result = add(5, 3);


[Link](result);

Output: 8
Explanation:
return a + b; sends the answer back.

 Function with Return (No Parameters)


function message() {
return "Hello Student";
}

[Link](message());

Output: Hello Student

 Function with Default Parameter


If no argument is given, default value is used.
function greet(name = "Student") {
[Link]("Hello " + name);
}
greet (); // Hello Student
greet("Ram"); // Hello Ram

 Super Quick MCQ Revision Table


Question Answer
A function is a block of ___? Code
Which keyword defines a function? function
Code inside function runs when? When called
Values passed to a function are? Arguments
Values received by a function are? Parameters
Which statement returns a value? return
Code after return executes? No
What is function call? Calling by name
Output of add(3,2) if function adds numbers? 5
Default parameter used when? No argument passed
Function definition means? Creating function
Function calling means? Running function

(Arrays in JavaScript)
An array is a collection of multiple values stored in a single variable.
Example:
var fruits = ["Apple", "Banana", "Orange"];

 Why Arrays Are Used?


 To store multiple values together
 To access values using index
 To reduce variable usage
Syntax
var arr = [value1, value2, value3];

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

 Changing Array Elements


fruits[1] = "Mango"; // ["Apple", "Mango", "Orange"]

 Array Length
Number of elements in array.
var size = [Link];
[Link](size); // 3

 Add Element (push)


Adds value at end.
[Link]("Kiwi"); // ["Apple", "Mango", "Orange", "Kiwi"]

 Remove Last Element (pop)


[Link] (); // ["Apple", "Mango", "Orange"]

 Add Element at Start (unshift)


[Link] ("Grapes"); // ["Grapes", "Apple", "Mango", "Orange"]

 Remove First Element (shift)


[Link] (); // ["Apple", "Mango", "Orange"]

 Check Array Type


[Link] (fruits); // true

 Important Array Methods


Method Meaning
push() Add at end
pop() Remove from end
shift() Remove from start
unshift() Add at start
length Total elements

 Loop through Array


for (var i = 0; i < [Link]; i++) {
[Link] (fruits[i]);
}

 Super Quick MCQ Revision Table


Question Answer
Array index starts from? 0
fruits[2] refers to? Third element
Method to add at end? push()
Method to remove last value? pop()
[Link] gives? Total elements
Method to remove first element? shift()

Method to add at start? unshift()


Output: ["a","b","c"].length 3
Is array a variable? Yes (special type)
How to check if variable is array? [Link]()
Index of first element? 0
Which method changes array size? push(), pop(), shift(), unshift()
var a=[] is? Empty array
Access second value? arr[1]
What does pop() return? Removed element

(DOM : Document Object Method)


The DOM allows JavaScript to access and change HTML elements.
It treats a webpage as a tree of objects (Document → Elements → Text).

 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

 Events in JavaScript (Important)


Events are actions like click, keypress, mouseover, etc.

 onclick Event
Triggered when the user clicks an element.
Syntax
<button Me</button>
Example
<button Me</button>
<p id="demo"></p>

<h1 changeH1(this)”>Hey buddy! Hit me </h1>

<script>
// Only changes <p> when button is clicked
function showMessage() {
[Link] ("demo").innerHTML = "Button Clicked!";
}

// Changes <h1> text when it is clicked


function changeH1(element) {
[Link]("demo").innerHTML = "H1 Clicked!";
[Link] = "Awesome Dude, You did it.";
}
</script>
Step-by-Step Output
 Click the button:
 <p> changes → Button Clicked!
 Button text stays → Click Me
 <h1> text stays → Hey buddy! Hit me

 Click the <h1>:


 <p> changes → H1 Clicked!
 <h1> text changes → Awesome Dude, You did it.

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]

 onmouseover and onmouseout Events


 onmouseover → triggers when mouse moves over an element.
 onmouseout → triggers when mouse leaves the element.
Syntax
<p ()" ()">Hover me</p>
Example
<p id="demo">
Move your mouse here
</p>
<script>
function mouseOver() {
[Link]("demo").innerHTML = "Mouse is over!";
}

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!

 onkeydown and onkeyup Events


 onkeydown → triggers when a key is pressed down.
 onkeyup → triggers when a key is released.
Syntax
<input type="text" >

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)

 Selecting HTML Elements


 getElementById
Selects one element using its ID.
Syntax
[Link]("id");
Example

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";

 Changing HTML Content


innerHTML is a property that allows JavaScript to change or set the content
inside an HTML element.
Syntax
[Link]("elementID").innerHTML = "New Content";
Example
HTML:
<p id="msg">Hello World</p>
<button Me</button>

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

 Changing CSS (Style)


[Link]("msg").[Link] = "red";
[Link]("msg").[Link] = "20px";
 Changing Image Source
HTML:

<img id="pic" src="[Link]">

JavaScript:
[Link]("pic").src = "[Link]";

 Changing Input Value


HTML:

<input id="name" type="text">

JavaScript:
[Link]("name").value = "Aditya";

 Show / Hide Elements


Hide

[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 b = " ";


[Link] (isNaN(b)); // false (empty space converts to 0)

var c = "";
[Link] (isNaN(c)); // false (empty string converts to 0)

 Super Quick MCQ Revision Table


Question Answer
DOM stands for? Document Object Model
Which method selects element by ID? getElementById
Which method selects by class? getElementsByClassName
Method to change HTML content? innerHTML
Method to change CSS? style
Method to check multiple matches? querySelectorAll
DOM treats a webpage as? Tree structure
Event that fires on button click? onclick
How to hide an element? [Link] = "none"
How to change image source? [Link]

You might also like