JavaScript Variables
What is a variable in JavaScript?
A variable in JavaScript is a named container used to store data values. Variables allow you to reference and manipulate data throughout your code.
How do you declare a variable in JavaScript?
You can declare a variable in JavaScript using the var, let, or const keywords.
var name = 'John'; // using var
let age = 30; // using let
const PI = 3.14; // using const
What is the difference between var, let, and const?
The differences are:
var:Function-scoped, can be re-declared and updated. It's hoisted to the top of the function.let:Block-scoped, can be updated but not re-declared in the same block.const:Block-scoped, cannot be updated or re-declared, and must be initialized at declaration.
What is hoisting in JavaScript?
Hoisting is a JavaScript mechanism where variable and function declarations are moved to the top of their containing scope during compilation. This means you can use variables before they are declared.
console.log(x); // undefined
var x = 5;
What happens if you try to use a variable that has not been declared?
If you try to use a variable that has not been declared, JavaScript will throw a ReferenceError.
console.log(y); // ReferenceError: y is not defined
How do you declare multiple variables in a single statement?
You can declare multiple variables in a single statement by separating them with commas.
let a = 1, b = 2, c = 3;
What is the scope of a variable?
The scope of a variable refers to the context in which it is defined and accessible. JavaScript has global, function, and block scopes:
- Global Scope: Variables declared outside any function are accessible from anywhere.
- Function Scope: Variables declared within a function are only accessible within that function.
- Block Scope: Variables declared with
letandconstwithin a block are only accessible within that block.
What is an undeclared variable in JavaScript?
An undeclared variable is a variable that has not been defined using var, let, or const. Assigning a value to an undeclared variable creates a global variable.
function myFunction() {
x = 10; // undeclared variable
}
myFunction();
console.log(x); // 10 (global variable)
What are template literals, and how do they relate to variables?
Template literals are string literals that allow for embedded expressions. They use backticks (`) and support multi-line strings and interpolation of variables.
const name = 'Alice';
const greeting = `Hello, ${name}!`;
console.log(greeting); // Hello, Alice!
What is the difference between a primitive variable and an object variable?
A primitive variable holds a single value (like a number or string), while an object variable holds a reference to an object, which can contain multiple values and complex data structures.
let primitive = 42; // primitive variable
let obj = { name: 'John' }; // object variable
How can you check the type of a variable in JavaScript?
You can check the type of a variable using the typeof operator, which returns a string indicating the type of the unevaluated operand.
console.log(typeof 'Hello'); // string
console.log(typeof 123); // number
console.log(typeof true); // boolean
console.log(typeof { name: 'Alice' }); // object
What are the rules for naming variables in JavaScript?
The rules for naming variables in JavaScript include:
- Variable names must begin with a letter, underscore (_), or dollar sign ($).
- Variable names can contain letters, numbers, underscores, and dollar signs.
- Variable names are case-sensitive.
- Variable names cannot be a reserved keyword (e.g.,
if,else,while, etc.).
What is a constant variable, and how do you declare one?
A constant variable is a variable that cannot be reassigned after its initial declaration. It is declared using the const keyword.
const PI = 3.14;
// PI = 3.14159; // This will throw an error
What is variable shadowing in JavaScript?
Variable shadowing occurs when a variable declared in a nested scope has the same name as a variable in an outer scope. The inner variable "shadows" the outer variable within its scope.
let x = 'outer';
function test() {
let x = 'inner';
console.log(x); // 'inner'
}
test();
console.log(x); // 'outer'