Day 1: JavaScript Basics โ€” Variables, Data Types & Operators ๐Ÿš€

Lokesh Prajapati
5 min read1 day ago

--

JavaScript Basicsโ€Šโ€”โ€ŠVariables, Data Types & Operators ๐Ÿš€

๐Ÿ‘‹ Welcome to the 30-Day JavaScript Learning Journey!

JavaScript is the backbone of web development and one of the most in-demand programming languages today. If you are just starting out or want to strengthen your JavaScript foundation, youโ€™re in the right place!

This is Day 1 of our 30-day JavaScript learning series, where we will start from the very basics and gradually move to advanced topics.

Todayโ€™s lesson is all about Variables, Data Types, and Operators โ€” the building blocks of JavaScript. By the end of this post, youโ€™ll be able to:
โœ… Declare and use variables correctly
โœ… Understand different types of data in JavaScript
โœ… Use operators to perform calculations and logic

Letโ€™s dive in! ๐Ÿš€

โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€”

๐Ÿ“Œ What Are Variables in JavaScript?

==> A variable is like a labeled container that holds a value. Imagine you have a box where you store your favorite snacks. You can open the box, check whatโ€™s inside, replace it with something else, or even lock it so nobody can change its contents.

In JavaScript, variables work in the same way. They store data that can be used and manipulated throughout the program.

๐ŸŸข How to Declare a Variable?

JavaScript provides three ways to declare a variable:
1๏ธโƒฃ var โ€“ The old way (not recommended)
2๏ธโƒฃ let โ€“ The modern and preferred way
3๏ธโƒฃ const โ€“ Used for constants (values that donโ€™t change)

var name = "TestUser";  // Old way (avoid using this)
let age = 25; // Modern way (preferred)
const country = "India"; // Cannot be changed

๐Ÿ”น Key Differences: var, let, and const

๐Ÿ”น Best Practices for Using Variables:

โœ… Use const whenever possible (for values that donโ€™t change).
โœ… Use let for variables that may change.
โœ… Avoid var due to scoping issues.
โœ… Give meaningful variable names (e.g., userName instead of x).

โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€”

๐Ÿ“Œ Understanding JavaScript Data Types

==> In JavaScript, data types define the kind of values a variable can hold. Since JavaScript is a loosely typed (or dynamically typed) language, a variable can hold different types of data without specifying the type explicitly.

๐Ÿ”น JavaScript Has Two Main Categories of Data Types

JavaScript data types are divided into two main categories:

1๏ธโƒฃ Primitive Data Types (Immutable, stored directly in memory)
2๏ธโƒฃ Non-Primitive (Reference) Data Types (Stored as references)

โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€”

1๏ธโƒฃ Primitive Data Types (Hold a single value) : Primitive data types are immutable (cannot be changed after creation) and are stored directly in memory.

JavaScript has 7 primitive data types:

  • ๐Ÿ“œ String - "Hello World"
  • ๐Ÿ”ข Number - 100, 3.14
  • โœ… Boolean - true, false
  • โ“ Undefined - Variable declared but no value assigned
  • โšซ Null - Intentionally empty
  • ๐ŸŽญ Symbol (Unique identifier)
  • ๐Ÿ”ข BigInt (For large numbers)

2๏ธโƒฃ Non-Primitive (Reference) Data Types (Hold complex values) : Non-primitive data types, also known as reference types, are objects and derived data types. They can store collections of values or more complex entities. The two key non-primitive data types in JavaScript are:

JavaScript has 3 primitive data types:

  • ๐Ÿ  Object - { name: "John", age: 30 }
  • ๐Ÿ“ฆ Array - [1, 2, 3, 4]
  • ๐ŸŽญ Function - function greet() { console.log("Hello!"); }

๐Ÿ” Example of Data Types in Action

let city = "New York";  // String
let temperature = 25.5; // Number
let isRaining = false; // Boolean
let fruits = ["Apple", "Mango", "Banana"]; // Array
let person = { name: "John", age: 30 }; // Object

console.log(typeof city); // "string"
console.log(typeof temperature); // "number"
console.log(typeof fruits); // "object"

๐Ÿ›  Tip: Use typeof to check the data type of any value!

โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€”

๐Ÿ“Œ JavaScript Operators โ€” Performing Operations

Just like a calculator, JavaScript uses operators to perform calculations and comparisons.

1๏ธโƒฃ Arithmetic Operators ๐Ÿงฎ (Math Operations) : Used for basic calculations.

let a = 10, b = 5;
console.log(a + b); // 15 (Addition)
console.log(a - b); // 5 (Subtraction)
console.log(a * b); // 50 (Multiplication)
console.log(a / b); // 2 (Division)
console.log(a % b); // 0 (Remainder)
console.log(a ** b); // 100000 (Exponentiation)

2๏ธโƒฃ Assignment Operators ๐Ÿ–Š (Assigning Values) : Used to assign values to variables.

let x = 10;
x += 5; // x = x + 5 (Now x = 15)
x -= 3; // x = x - 3 (Now x = 12)
x *= 2; // x = x * 2 (Now x = 24)
x /= 4; // x = x / 4 (Now x = 6)

3๏ธโƒฃ Comparison Operators ๐Ÿ”„ (Checking Conditions) : Used to compare values.

console.log(10 == "10");  // true  (Checks value only)
console.log(10 === "10"); // false (Checks value and type)
console.log(10 != 5); // true (Not equal)
console.log(10 > 5); // true (Greater than)
console.log(10 < 5); // false (Less than)

4๏ธโƒฃ Logical Operators ๐Ÿคฏ (Combining Conditions) : Used to make decisions.

let isSunny = true, isWarm = false;
console.log(isSunny && isWarm); // false (AND: both must be true)
console.log(isSunny || isWarm); // true (OR: one must be true)
console.log(!isSunny); // false (NOT: reverses boolean value)

โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€”

๐ŸŽฏ Real-World Example โ€” Shopping Cart Calculation

Letโ€™s put everything we learned into a simple shopping cart total price calculator:

let price = 500;
let discount = 10; // 10% discount

let finalPrice = price - (price * discount / 100);

console.log(`Final Price after Discount: $${finalPrice}`);
// Final Price after Discount: $450.

๐Ÿ› Scenario: You have a product worth $500, and a 10% discount applies. The final price is calculated as $450.

โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€”

๐Ÿ”š Conclusion

Today, we learned:
โœ… How to declare variables using var, let, and const
โœ… The difference between primitive and reference data types
โœ… How to use arithmetic, assignment, comparison, and logical operators

๐Ÿ”น This knowledge is essential for writing JavaScript programs and will be the base for all future topics.

โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€”

๐Ÿ“Œ Next Up: Day 2 โ€” JavaScript Functions & Scope

๐Ÿ’ฌ Did you learn something new today? Let me know in the comments!

๐Ÿ”” Follow me to stay updated for the next 29 days of JavaScript learning!

--

--

Lokesh Prajapati
Lokesh Prajapati

Written by Lokesh Prajapati

Front-end-developer | Web Designer | Shopify Experts - https://lokesh-prajapati.vercel.app/

No responses yet