JavaScript Documentation

1.1 What is JavaScript

- JavaScript (JS) is a high-level, interpreted, multi-paradigm language.
- Originally created to add interactivity to web pages (pop-ups, form validation, animations)
- Now it runs everywhere: Browsers, Servers, Apps etc.
- Created by Brendan Eich in 1995, earlier known as Mocha, then LiveScript, then JavaScript.

Famous Websites that run on JavaScript: Google, Facebook, Youtube, Netflix.

1.2 How Javascript runs

Suppose when we open a Web Page
- The browser downloads the HTML, CSS and JS files.
- It parses (reads) the HTML.
- When it finds a <script> tag and sends the JS code the JavaScript Engine.
- The engine compiles and runs the JavaScript code.

In Chrome the JavaScript Engine is called V8.

Example Code as in a HTML file.
samplePage.html
1<!DOCTYPE html> 2<html lang="en"> 3<head> 4 <meta charset="UTF-8"> 5 <title>JavaScript Basics</title> 6</head> 7<body> 8 <h1>JavaScript Test</h1> 9 <script> 10 console.log("JavaScript inside HTML!"); 11 alert("Welcome to JavaScript!"); 12 </script> 13</body> 14</html>

On loading the page you will find an alert saying "Welcome to JavaScript!".
On going to the Console Tab from Inspect you will find the message "JavaScript inside HTML!"

You can also directly write this code in Console.
console.js
1console.log("Hello from the Console")

1.3 Basic JavaScript Syntax

Comment Lines:
comments.js
1// This is a single-line comment 2/* This is a 3 multi-line comment */

Logging Output:
output.js
1console.log("This goes to the browser console")

Alert Popup:
alert.js
1alert("This is an alert box!")

Variables:
variables.js
1let name = "Oliver" 2const age = 17

1.4 JavaScript variables

A variable is a container that stores data which you can use and manipulate later.
It is to be understood as a labelled box.

Example of Variable
variable.js
1let message = "Hello, World!"
Here Hello, World! is stored in a box labelled message.

Declaration and Scope of Variables

var
- Reassign: Yes ✅
- Scope: Function-scoped
- Hoisting: Yes ✅
- Best Use: Avoid (legacy code) ❌

let
- Reassign: Yes ✅
- Scope: Block-scoped
- Hoisting: No ❌
- Best Use: Use for changeable variables ✅

const
- Reassign: No ❌
- Scope: Block-scoped
- Hoisting: No ❌
- Best Use: Use when value should not change ✅

We generally use let and const in modern JavaScript.

Usage of let
letKeyword.js
1let score = 10 2score = 20 // allowed

Usage of const
constKeyword.js
1const pi = 3.14 2pi = 3.14159 // error

1.5 Data Types in JavaScript

Primitive Data Types - These hold only single values

- String - ( "Hello" , 'JavaScript' )
- Number - ( 10 , 3.14 , -5 )
- Boolean - ( true , false )
- Undefined - ( A declared variable with no value )
- Null - ( An intentional empty value )
- BigInt - ( Very large number )
- Symbol - ( Unique and Immutable value )

Example of different Data Types
dataTypes.js
1let str = "Oliver" // String 2let num = 42 // Number 3let isStudent = true // Boolean 4let x // Undefined 5let y = null // Null

Non-Primitive (Reference) Data Types - These hold primitive values

- Object
- Array
- Function

To be discussed later

The typeof Operator - to check the type of any variable

Usage of typeof Operator:
typeof.js
1console.log(typeof "Oliver") // string 2console.log(typeof 10) // number 3console.log(typeof true) // boolean 4console.log(typeof undefined) // undefined 5console.log(typeof null) // object (weird quirk!)

Caution: null is intentional, undefined is not

1.6 Operators and Expressions in JavaScript

Expression is any valid unit of code that evaluates to a value

3 + 5 = 8
"Hello" + "World" = "HelloWorld"
true && false = false

We write expressions using operators and values

Arithmatic Operators - Used for mathematical operations

OperatorDescriptionExampleResult
+Addition10 + 515
-Subtraction10 - 55
*Multiplication10 * 550
/Division10 / 52
%Modulus (Remainder)10 % 31
**Exponentiation2 ** 38

Arithmatic Operators in code
arithmatic.js
1let a = 10 2let b = 3 3console.log("Addition:", a + b) 4console.log("Modulo:", a % b) 5console.log("Power:", a ** b)

Assignment Operators - Used to assign values to variables

OperatorDescriptionExampleResult
=Assignmentx = 10x = 10
+=Add and Assignx += 2x = x + 2
-=Subtract and Assignx -= 3x = x - 3
*=Multiply and Assignx *= 2x = x * 2
/=Divide and Assignx /= 2x = x / 2

Assignment Operators in code
assignment.js
1let score = 20 2score += 5 3console.log("Score after bonus:", score)


Comparision Operators - Used to compare values then return true or false

OperatorDescriptionExampleResult
==Equal (lose)5 == '5'true
===Equal (strict)5 === '5'false
!=Not Equal (loose)5 != '5'false
!==Not Equal (strict)5 !== '5'true
>Greater than5 > 3true
<Less than3 < 5true
>=Greater than or equal to5 >= 5true
<=Less than or equal to4 <= 5true

Comparision Operators in code
comparision.js
1console.log(10 > 5) // true 2console.log(10 === "10") // false 3console.log(10 == "10") // true


Logical Operators - Used to combine conditions
OperatorDescriptionExampleResult
&&ANDtrue && falsefalse
||ORtrue || falsetrue
!NOT!truefalse

Logical Operators in code
logical.js
1let isLoggedIn = true 2let hasPremium = false 3console.log(isLoggedIn && hasPremium) // false 4console.log(isLoggedIn || hasPremium) // true 5console.log(!isLoggedIn) // false


1.7 Conditionals in JavaScript

JavaScript lets our code respond differently based on user inputs, values or situations

Suppose we get to vote at 18
So if our age is 18 or above we are eligible else we are not

Basic Syntax system of Conditionals
conditionals.js
1if (condition) { 2 // runs if condition is true 3} else if (anotherCondition) { 4 // runs if anotherCondition is true 5} else { 6 // runs if nothing else is true 7}

For example we want to build an Age Checker
age.js
1let age = 20 2if (age < 13) { 3 console.log("Child") 4} else if (age < 18) { 5 console.log("Teenager") 6} else { 7 console.log("Adult") 8}

Conditionals Statements can also be combined with Logical Operators

For example we want to build a Grade Assigner from obtained marks
marks.js
1let marks = 75 2if (marks >= 90 && marks <= 100) { 3 console.log("Grade: A+") 4} else if (marks >= 75 && marks < 90) { 5 console.log("Grade: A") 6} else if (marks >= 60 && marks < 75) { 7 console.log("Grade: B") 8} else { 9 console.log("Grade: C or below") 10}

Switch Statement - It is used when we have many exact matches to check

For example we want to build a day in the week finder
dayOfWeek.js
1let day = 3 2switch (day) { 3 case 1: console.log("Monday") break 4 case 2: console.log("Tuesday") break 5 case 3: console.log("Wednesday") break 6 case 4: console.log("Thursday") break 7 case 5: console.log("Friday") break 8 case 6: console.log("Saturday") break 9 case 7: console.log("Sunday") break 10 default: console.log("Invalid day") 11}

1.8 Loops in JavaScript