JavaScript Variables & Data Types: var, let, const, Hoisting, Primitives & Type Coercion
1. What is a variable in JavaScript?
Q: What is a variable in JavaScript?
A variable is a named storage location for holding data that can be modified during program execution. Variables allow developers to store, retrieve, and manipulate values in JavaScript.
2. How are variables declared in JavaScript?
Q: How are variables declared?
Variables are declared using var, let, or const, followed by a name (e.g., let age = 25;). Each keyword has specific scoping and reassignment rules.
3. Why do we use variables in programming?
Q: Why use variables?
Variables are used to store data (like numbers, text, or objects) for reuse, manipulation, or tracking state in a program, making code more flexible and maintainable.
4. What is the var keyword used for?
Q: What is var?
var declares a variable with function scope or global scope. It allows redeclaration and reassignment but is less predictable due to hoisting and scope issues (e.g., var x = 10; var x = 20;).
5. What is the let keyword used for?
Q: What is let?
let declares a block-scoped variable that can be reassigned but not redeclared within the same scope (e.g., let y = 15; y = 20; is OK, but let y = 25; throws an error).
6. What is the const keyword used for?
Q: What is const?
const declares a block-scoped variable that cannot be reassigned or redeclared after initialization. However, object properties or array elements declared with const can be modified (e.g., const z = 30; z = 40; throws an error).
7. What is the difference between var, let, and const?
Q: Difference between var, let, const?
var: Function-scoped, allows redeclaration and reassignment, hoisted to the top of its scope.let: Block-scoped, allows reassignment but not redeclaration, not hoisted.const: Block-scoped, no reassignment or redeclaration, not hoisted, but allows mutable object properties to change.
8. What is hoisting in JavaScript?
Q: What is hoisting?
Hoisting is JavaScript's behavior of moving var declarations (but not initializations) to the top of their scope during execution, which can lead to unexpected behavior (e.g., console.log(x); var x = 5; outputs undefined).
9. When should you use let vs. const?
Q: When to use let vs const?
Use let when the variable's value needs to change. Use const for values that should remain constant or for objects/arrays where the reference won't change but properties/elements might.
10. Can you give an example of var, let, and const declarations?
Q: Example of var, let, const?
var name = "Alice"; // Function-scoped, redeclarable
let age = 25; // Block-scoped, reassignable
const PI = 3.14; // Block-scoped, cannot be reassigned
name = "Bob"; // OK
age = 30; // OK
// PI = 3.14159; // Error: Assignment to constant
11. What happens if you try to redeclare a variable with let or const?
Q: Redeclaring let or const?
Redeclaring a variable with let or const in the same block scope throws a SyntaxError (e.g., let x = 10; let x = 20; causes an error).
12. What is block scope vs. function scope?
Q: Block scope vs function scope?
- Block scope: Variables declared with
letorconstare limited to the block ({}) they are defined in (e.g., inside aniforforblock). - Function scope: Variables declared with
varare scoped to the entire function or globally if outside a function.
13. What are data types in JavaScript?
Q: What are data types in JS?
Data types define the kind of data a variable can hold. JavaScript has primitive types (Number, String, Boolean, null, undefined, Symbol, BigInt) and non-primitive types (e.g., Object, Array).
14. What is the Number data type?
Q: What is Number type?
The Number type represents both integers and floating-point numbers (e.g., 42, 3.14). It also includes special values like Infinity, -Infinity, and NaN (Not a Number).
15. What is the String data type?
Q: What is String type?
The String type represents sequences of characters, enclosed in single quotes ('), double quotes ("), or backticks (`) for template literals (e.g., "Hello", 'World', `Hi, ${name}`).
16. What is the Boolean data type?
Q: What is Boolean type?
The Boolean type represents a logical value, either true or false, often used in conditional statements (e.g., let isActive = true;).
17. What is the null data type?
Q: What is null?
null represents the intentional absence of any value or object. It's used to indicate that a variable has no value (e.g., let data = null;).
18. What is the undefined data type?
Q: What is undefined?
undefined indicates a variable that has been declared but not assigned a value (e.g., let x; console.log(x); outputs undefined).
19. What is the Symbol data type?
Q: What is Symbol?
Symbol is a unique and immutable primitive introduced in ES6, often used as unique object keys to avoid property name collisions (e.g., const id = Symbol('id');).
20. What is the BigInt data type?
Q: What is BigInt?
BigInt is a primitive type for representing integers larger than the Number type's safe limit (2^53 - 1). It's created by appending n to an integer or using the BigInt() constructor (e.g., let bigNum = 12345678901234567890n;).
21. What are the differences between null and undefined?
Q: null vs undefined?
null: Explicitly set to indicate no value (e.g.,let x = null;).undefined: Automatically assigned to uninitialized variables or missing function returns.typeof nullreturns"object"(historical quirk), whiletypeof undefinedreturns"undefined".
22. Can you provide examples of all primitive data types?
Q: Examples of primitive types?
let num = 42; // Number
let str = "Hello, World!"; // String
let isTrue = true; // Boolean
let nothing = null; // null
let notAssigned; // undefined
let sym = Symbol('unique'); // Symbol
let bigNum = 123456789012345n; // BigInt
console.log(num, str, isTrue, nothing, notAssigned, sym, bigNum);
23. How can you check the data type of a variable in JavaScript?
Q: How to check data type?
Use the typeof operator to check a variable's data type (e.g., typeof 42; returns "number", typeof "hello"; returns "string").
24. What are some common operations with Number and String types?
Q: Operations with Number and String?
Number: Arithmetic operations (+,-,*,/), comparison (>,<), and methods liketoFixed()(e.g.,(3.14159).toFixed(2);returns"3.14").String: Concatenation (+), length ("hello".length;returns5), and methods liketoUpperCase(),slice(), orreplace().
25. Why is BigInt useful?
Q: Why use BigInt?
BigInt is useful for handling large integers (e.g., in financial calculations or cryptography) where Number might lose precision due to its limit of 2^53 - 1.
26. Are JavaScript data types dynamically or statically typed?
Q: Dynamic or static typing?
JavaScript is dynamically typed, meaning variables can hold any data type without explicit type declaration, and types can change at runtime (e.g., let x = 5; x = "text"; is valid).
27. What is type coercion in JavaScript?
Q: What is type coercion?
Type coercion is JavaScript's automatic conversion of one data type to another during operations (e.g., "5" + 3; returns "53" (string) or 5 + true; returns 6 (true coerced to 1)).
28. What is the difference between primitive and non-primitive data types?
Q: Primitive vs non-primitive?
- Primitive: Immutable, simple data types (
Number,String,Boolean,null,undefined,Symbol,BigInt) stored directly in memory. - Non-primitive: Mutable, complex types like
Object,Array, orFunction, stored as references in memory.