Variables
=========
[Link] Variable Declaration
--------------------------
var name = "Hareesh";
let age = 25;
const country = "India";
[Link]("Name:", name);
[Link]("Age:", age);
[Link]("Country:", country);
[Link] and Redeclaration
----------------------------------
// var can be redeclared and reassigned
var city = "Hyderabad";
var city = "Bangalore"; // redeclaration allowed
[Link](city);
// let can be reassigned but not redeclared
let language = "JavaScript";
language = "TypeScript"; // ✅ allowed
// let language = "Python"; ❌ error
[Link](language);
// const cannot be reassigned or redeclared
const pi = 3.14;
// pi = 3.1415; ❌ error
[Link](pi);
3. Block Scope Example
----------------------
{
var x = 10; // function/global scope
let y = 20; // block scope
const z = 30; // block scope
[Link]("Inside block:", x, y, z);
}
[Link]("Outside block:", x);
// [Link](y, z); // ❌ error: y, z not defined
4. Hoisting Behavior
---------------------
[Link](a); // undefined (var is hoisted)
var a = 5;
// [Link](b); // ❌ ReferenceError (let and const not hoisted)
let b = 10;
5. Using Variables in Expressions
---------------------------------
let num1 = 10;
let num2 = 20;
let sum = num1 + num2;
let message = "The sum is " + sum;
[Link](message);
6. Swap Two Variables
----------------------
let a = 5;
let b = 10;
[Link]("Before swap:", a, b);
let temp = a;
a = b;
b = temp;
[Link]("After swap:", a, b);
7. Variable with Template Literals
----------------------------------
let firstName = "Hareesh";
let lastName = "Reddy";
let fullName = `${firstName} ${lastName}`;
[Link](`Hello, ${fullName}!`);
8. Working with Constants (Objects & Arrays)
--------------------------------------------
const person = { name: "Ravi", age: 22 };
[Link] = 23; // ✅ allowed (modifying object property)
[Link](person);
const numbers = [1, 2, 3];
[Link](4); // ✅ allowed
[Link](numbers);
9. Practical Example: Calculate Total Bill
------------------------------------------
let price = 100;
let quantity = 5;
const tax = 0.18;
let total = price * quantity;
let totalWithTax = total + total * tax;
[Link](`Total Bill: ₹${totalWithTax}`);
---------------------------------------------------------------------------------
Data Types
===========
[Link]
----------
Represents text, written inside quotes.
let name = "Hareesh";
let greeting = 'Hello';
let message = `Welcome, ${name}!`;
[Link](name);
[Link](typeof name); // string
[Link](message);
2. Number
----------
Used for integers, decimals, etc.
let age = 25;
let price = 99.99;
[Link](age, price);
[Link](typeof price); // number
[Link]
---------
Represents true or false.
let isLoggedIn = true;
let hasPermission = false;
[Link](isLoggedIn, hasPermission);
[Link](typeof isLoggedIn); // boolean
4. Undefined
------------
A variable declared but not assigned any value.
let city;
[Link](city); // undefined
[Link](typeof city); // undefined
5. Null
-----------
Intentional absence of any value.
let user = null;
[Link](user); // null
[Link](typeof user); // object (JavaScript quirk)
6. Object
---------
Used to store data in key–value pairs.
let person = {
name: "Ravi",
age: 25,
city: "Hyderabad"
};
[Link](person);
[Link]([Link]);
[Link](typeof person); // object
7. Array
--------
Used to store multiple values in one variable.
let colors = ["red", "green", "blue"];
[Link](colors);
[Link](colors[1]); // green
[Link](typeof colors); // object
identify data type
---------
let data = 10;
[Link](typeof data); // number
data = "Now I am a string";
[Link](typeof data); // string
data = true;
[Link](typeof data); // boolean
Operators
==========
What Are Operators?
Operators perform actions on operands (variables or values).
Example:
let x = 10 + 5; // '+' is the operator, 10 and 5 are operands
1. Arithmetic Operators
-------------------------
Used for mathematical calculations
| Operator | Description | Example | Output |
| -------- | ------------------- | -------- | -------------- |
| `+` | Addition | `10 + 5` | `15` |
| `-` | Subtraction | `10 - 5` | `5` |
| `*` | Multiplication | `10 * 5` | `50` |
| `/` | Division | `10 / 5` | `2` |
| `%` | Modulus (remainder) | `10 % 3` | `1` |
| `**` | Exponentiation | `2 ** 3` | `8` |
| `++` | Increment | `x++` | Increases by 1 |
| `--` | Decrement | `x--` | Decreases by 1 |
examples
let a = 10, b = 3;
[Link](a + b); // 13
[Link](a - b); // 7
[Link](a * b); // 30
[Link](a / b); // 3.333...
[Link](a % b); // 1
[Link](a ** b); // 1000
2. Assignment Operators
------------------------
Assign values to variables.
| Operator | Example | Equivalent To |
| -------- | -------- | ------------- |
| `=` | `x = 5` | `x = 5` |
| `+=` | `x += 2` | `x = x + 2` |
| `-=` | `x -= 2` | `x = x - 2` |
| `*=` | `x *= 2` | `x = x * 2` |
| `/=` | `x /= 2` | `x = x / 2` |
| `%=` | `x %= 2` | `x = x % 2` |
Example
let x = 10;
x += 5;
[Link](x); // 15
x *= 2;
[Link](x); // 30
3. Comparison Operators
-----------------------
Used to compare values (return true or false).
| Operator | Description | Example | Output |
| -------- | --------------------- | ------------- | ------- |
| `==` | Equal to | `10 == '10'` | `true` |
| `===` | Equal + same type | `10 === '10'` | `false` |
| `!=` | Not equal | `10 != 5` | `true` |
| `!==` | Not equal + same type | `10 !== '10'` | `true` |
| `>` | Greater than | `10 > 5` | `true` |
| `<` | Less than | `10 < 5` | `false` |
| `>=` | Greater than or equal | `10 >= 10` | `true` |
| `<=` | Less than or equal | `5 <= 10` | `true` |
Example
[Link](10 == "10"); // true
[Link](10 === "10"); // false
[Link](10 != 5); // true
[Link](10 !== "10"); // true
4. Logical Operators
----------------------
Used to combine conditions.
| Operator | Description | Example | Output | | | |
|
| -------- | ----------- | --------------- | ------- | ----- | - | ------ | ------
|
| `&&` | AND | `true && false` | `false` | | | |
|
| ` | | ` | OR | `true | | false` | `true`
|
| `!` | NOT | `!true` | `false` | | | |
|
examples
let age = 20;
let hasLicense = true;
[Link](age >= 18 && hasLicense); // true
[Link](age >= 18 || hasLicense); // true
[Link](!hasLicense); // false
5. String Operators
---------------------
Used to join strings.
let first = "Hello";
let second = "World";
let result = first + " " + second;
[Link](result); // Hello World
6. Ternary Operator
-------------------
Short form of if...else.
let age = 18;
let message = (age >= 18) ? "Adult" : "Minor";
[Link](message);
--------------------------------------------------------------------------------
Comments
=========
1. Single-Line Comment
-----------------------
Use // to write a comment on one line.
// This is a single-line comment
let x = 10; // Assigning 10 to variable x
[Link](x); // Printing the value of x
2. Multi-Line Comment
------------------
Use /* ... */ for comments that span multiple lines.
/*
This is a multi-line comment.
It can describe code in detail.
Useful for explaining complex logic.
*/
let y = 20;
[Link](y);