๐Ÿง  10-Day JavaScript Learning Challenge - Day - 2

Smriti SinghSmriti Singh
3 min read

#webdev #javascript #learningjs #programming

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)

  1. What is the difference between primitive and non-primitive data types in JavaScript?

  2. What are objects and arrays? How are they different?

  3. What is the result of 5 == "5" and 5 === "5"? Why?

  4. What does the && operator do? When would you use it?

  5. 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.

0
Subscribe to my newsletter

Read articles from Smriti Singh directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Smriti Singh
Smriti Singh

๐Ÿ‘ฉโ€๐Ÿ’ป Frontend Developer | Learning Full-Stack | Exploring Web3 I enjoy building easy-to-use websites and apps with a focus on clean UI/UX. Currently expanding into full-stack development to grow my skillset. Iโ€™ve worked on exciting Web3 projects and love exploring how blockchain can shape the future of the web.