JavaScript
Introduction:
JavaScript is a high-level, lightweight, and interpreted programming language mainly used to make web pages
interactive and dynamic. It is one of the core technologies of web development, along with HTML and CSS.
JavaScript Environment Setup:
To start writing and running JavaScript code, you don’t need to install any special compiler or software. You only
need two basic tools:
1. A Code Editor – to write the code
2. A Web Browser – to run and test the code
Variables:
Variables are containers for storing data values. In JavaScript variables allows us to store, update and reuse data
throughout the code.
How to Declare a variable:
In JavaScript, you can declare a variable using one of the following keywords:
var (old way not recommended)
let (Modern way and commonly used)
const (For constants - values that doesn’t change)
let city = "Delhi";
city = "Mumbai"; // ✅ Reassignment allowed
const age = 25;
age = 30; // ❌
Error: Assignment to constant variable.
var name = "Code";
var name = "Sphere"; // ✅ Redeclaration allowed with var
let a = 10;
let a = 20; // ❌ SyntaxError: Identifier 'a' has already been declared
a = 20; // ✅ This is reassignment, and it's allowed
Data Types:
Data types are the types of values that can be stored and manipulated in a program.
JavaScript is a dynamically typed language, which means you don’t have to declare the type of variable — it’s
automatically determined based on the value assigned.
Types of Data in JavaScript:
JavaScript has two main categories:
JavaScript 1
Primitive Data Types (Basic types)
Data Type Example Description
String "Hello" Text value inside quotes
Number 10, 3.14 Integer or decimal
Boolean true, false Logical (yes/no, on/off)
Null null Intentionally empty
Undefined undefined Variable declared but no value assigned
BigInt 12345678901234567890n Large integers beyond normal number limits
Symbol Symbol('id') Unique, immutable value used as identifier
Non-Primitive (Reference) Data Types
Data Type Example Description
Object {name: "Raj", age: 20} Collection of key-value pairs
Array [1, 2, 3, 4] List of elements (indexed)
Function function greet() {} A block of code you can call
NOTE: In JavaScript, arrays and functions are technically objects.
// Primitive
let fruit = "apple"; // String
let n = 5; // Number
let isStudent = true; // Boolean
let empty = null; // Null
let notAssigned; // Undefined
let bigNum = 12345678901234567890n; // BigInt
let uniqueId = Symbol("id"); // Symbol
// Non-Primitive
let person = { name: "Code Sphere", series: "JavaScript" }; // Object
let numbers = [1, 2, 3, 4]; // Array
function greet() { [Link]("Hello"); } // Function
Type Casting:
Type casting in JavaScript (also known as type conversion) is the process of converting a value from one data
type to another
There are two types of type casting in JavaScript:
Implicit Type Casting (Automatic Conversion)
Explicit Type Casting (Manual Conversion)
1. Implicit Type Casting:
JavaScript automatically converts types behind the scenes when needed.
Operation Conversion Type Example Result
"5" + 1 Number → String "5" + "1" “51”
JavaScript 2
Operation Conversion Type Example Result
"5" - 1 String → Number 5-1 4
“5” * 2 String → Number 5-1 10
true + 1 Boolean → Number 1+1 2
false + "hi" Boolean → String "falsehi" “falsehi”
null + 1 null → 0 0+1 1
2. Explicit Type Casting:
You manually convert a value from one type to another using built-in functions.
From → To Method
Number → String String(n) or [Link]()
String → Number Number(str), parseInt(str), parseFloat(str)
Any → Boolean Boolean(value)
let num = 123;
let str = String(num); // or [Link]()
let str = "456";
let num = Number(str);
Boolean("") // false
Boolean("hello") // true
Boolean(0) // false
Boolean(42) // true
Number("abc") // Not a Number
"5" + 1 // "51"
"5" - 1 // 4
true + 1 // 2 (true is 1)
false + "1" // "false1" (false becomes string)
JavaScript 3