Day 2: Variables and Data Types in JavaScript
Welcome to Day 2 of learning JavaScript! Today, we’ll explore the building blocks of any program: variables and data types. These concepts are crucial as they form the foundation for everything you do in JavaScript.
What Are Variables?
A variable is like a container that holds data values. Think of it as a labeled box where you can store information, retrieve it later, or even change its contents.
Declaring Variables in JavaScript
JavaScript provides three ways to declare variables:
-
var
- The old way (avoid using this unless necessary). -
let
- Recommended for variables that can change. -
const
- For variables that shouldn’t change (constants).
Example:
var oldWay = "Avoid this if possible";
let currentWay = "Use let for variables that can change";
const fixedValue = "Use const for constants";
Difference Between var
, let
, and const
Feature | var |
let |
const |
---|---|---|---|
Scope | Function-scoped | Block-scoped | Block-scoped |
Reassignable | Yes | Yes | No |
Redeclarable | Yes | No | No |
Example:
function scopeTest() {
if (true) {
var x = "Function scope";
let y = "Block scope";
const z = "Constant";
}
console.log(x); // Accessible
// console.log(y); // Error: y is not defined
// console.log(z); // Error: z is not defined
}
scopeTest();
JavaScript Data Types
JavaScript has two types of data: Primitive and Non-Primitive.
Primitive Data Types
- String: Textual data. Example:
let name = "Arjun";
console.log(name); // "Arjun"
- Number: Numeric data. Example:
let age = 22;
console.log(age); // 22
- Boolean: True or false values. Example:
let isStart_up_guy = true;
console.log(isStart_up_guy); // true
- Null: Represents an intentional absence of value. Example:
let emptyValue = null;
console.log(emptyValue); // null
- Undefined: A variable that has been declared but not assigned a value. Example:
let uninitialized;
console.log(uninitialized); // undefined
- Symbol: A unique and immutable value. Example:
let uniqueKey = Symbol("key");
console.log(uniqueKey); // Symbol(key)
Type Conversion
JavaScript allows you to convert values between types.
Implicit Conversion (Coercion)
JavaScript sometimes converts types automatically.
Example:
let result = "5" + 5; // String + Number
console.log(result); // "55" (string)
Explicit Conversion
You can manually convert types using built-in functions like Number()
, String()
, or Boolean()
.
Example:
let num = "42";
let convertedNum = Number(num);
console.log(convertedNum); // 42 (number)
let boolValue = Boolean(0);
console.log(boolValue); // false
Practice for Today
-
Declare variables using
let
andconst
to store:- Your favorite book name.
- The number of books you own.
- Whether you like reading.
-
Try type conversion:
- Convert a number to a string.
- Convert a string to a number.
Summary of Day 2
Today, we covered:
-
Variables:
var
,let
, andconst
. - Primitive Data Types: String, Number, Boolean, Null, Undefined, and Symbol.
- Type Conversion: Implicit and explicit ways to convert types in JavaScript.
Next Steps
Tomorrow, we’ll dive into operators and expressions in JavaScript to start manipulating data and writing more complex programs. Stay tuned for Day 3: Operators and Expressions!
Top comments (0)