Day 1: Understanding Variables and Data Types
Today, I took my first step into JavaScript by learning about variables, their rules, the differences between var and let, and explored primitive data types and objects.
Variables in JavaScript
Variables are used to store data, and in JavaScript, we can declare them using var, let, or const.
Here’s what I learned:
var: It was traditionally used but has issues like function scoping and hoisting.
let: Introduced in ES6, it allows block scoping, making it safer than var.
const: Used for values that shouldn't change after being assigned.
Primitive and Non-Primitive Data Types
JavaScript data types are classified into primitive and non-primitive (reference) types.
Primitive Data Types
JavaScript has six main primitive data types:
1. String – Represents text ("Hello, world!")
2. Number – Includes integers and floating-point numbers (42, 3.14)
3. Boolean – Represents true/false values (true, false)
4. Undefined – A variable that has been declared but not assigned a value
5. Null – Represents an intentional absence of value
6. Symbol – Unique and immutable (used for object properties)
7. BigInt – Used for very large numbers beyond the Number limit
Non-Primitive Data Types: Objects
Unlike primitive data types, objects are non-primitive because they store collections of data and are referenced in memory rather than directly holding a value.
Example of an object:
let person = {
name: "Haleema",
age: 18,
isLearningJS: true
};
console.log(person.name); // Output: Haleema
Objects can store multiple properties, and their values can be modified after creation.
RESOURCE: CodeWithHarry