๐ Day 2: Data Types & Operators
Welcome back to Day 2 of the JavaScript Learning Challenge! ๐
Now that you've learned how to set up JavaScript and print output in the console, itโs time to dive deeper into one of the most fundamental aspects of JavaScript: Data Types and Operators.
๐ข JavaScript Data Types
In JavaScript, everything is a value. Every value has a type, and JavaScript has two broad categories:
โ
1. Primitive Data Types
These are basic and immutable:
String โ Text inside quotes
let name = "Smriti";
Number โ Whole numbers or decimals
let age = 25;
let score = 98.6;
Boolean โ true or false
let isLoggedIn = true;
Undefined โ A variable declared but not assigned a value
let email;
console.log(email); // undefined
Null โ Explicitly empty value
let address = null;
โ
2. Non-Primitive (Reference) Data Types
These are mutable and store references to values (objects in memory):
๐งฑ Object
A collection of key-value pairs.
let person = {
name: "Smriti",
age: 25,
isStudent: true
};
๐ฆ Array
An ordered list of values (indexed from 0).
let fruits = ["apple", "banana", "mango"];
๐ Function
Functions are also objects!
function greet() {
console.log("Hello, world!");
}
Unlike primitive types, non-primitive types are copied by reference โ changing one changes the original.
๐งฎ JavaScript Operators
โ
1. Arithmetic Operators
Used for math operations:
let a = 10;
let b = 5;
console.log(a + b); // 15
console.log(a - b); // 5
console.log(a * b); // 50
console.log(a / b); // 2
console.log(a % b); // 0
โ
2. Assignment Operators
Used to assign or update variable values:
let x = 10;
x += 5; // x = x + 5
x -= 2; // x = x - 2
โ
3. Comparison Operators
Used to compare values. Returns a boolean (true or false).
console.log(10 == "10"); // true (loose equality)
console.log(10 === "10"); // false (strict equality)
console.log(5 != 3); // true
console.log(5 < 10); // true
console.log(5 >= 5); // true
โ
4. Logical Operators
Used to combine boolean values:
let isStudent = true;
let hasID = false;
console.log(isStudent && hasID); // false (AND)
console.log(isStudent || hasID); // true (OR)
console.log(!isStudent); // false (NOT)
โ
Mini Task: Create a Simple Calculator
Letโs build a simple calculator using the arithmetic operators we learned today.
Example:
let num1 = 12;
let num2 = 4;
console.log("Addition:", num1 + num2);
console.log("Subtraction:", num1 - num2);
console.log("Multiplication:", num1 * num2);
console.log("Division:", num1 / num2);
console.log("Remainder:", num1 % num2);
๐ก Try modifying the values of num1 and num2 and re-running the code.
You can even extend this to ask for user input using prompt() (in browsers)!
โ Interview Questions (Day 2 Topics)
- What is the difference between primitive and non-primitive data types in JavaScript?
- What are objects and arrays? How are they different?
- What is the result of 5 == "5" and 5 === "5"? Why?
- What does the && operator do? When would you use it?
- How are variables assigned by value vs. reference in JavaScript?
๐ Congrats on completing Day 2!
Tomorrow in Day 3, weโll explore Conditional Statements and learn how to make decisions in your code using if, else, and switch.
Top comments (0)