JavaScript: Variables and Data Types
1. Variable Declarations
JavaScript uses `var`, `let`, and `const` to declare variables:
- `let` is block scoped and allows reassignment.
- `const` is block scoped and cannot be reassigned.
- `var` is function scoped (older; not recommended).
2. Data Types
JavaScript is dynamically typed. Common data types include:
- Primitive: String, Number, Boolean, Null, Undefined, BigInt, Symbol
- Non-Primitive: Object, Array, Function
3. typeof Operator
The `typeof` operator returns the type of a variable.
Example: `typeof "hello"` returns "string".
Practice Questions
Q1: Declare a variable using `let` to store your favorite programming language.
Q2: Declare a constant using `const` to store the name of your country.
Q3: Create an object `book` with keys: `title`, `author`, and `pages`.
Q4: Create an array `colors` with at least 3 color names.
Q5: Use `typeof` to print the data types of your variables from Q1, Q2, Q3, and Q4.
Q6: What will be the output of `typeof null` and why is it considered a bug in JavaScript?