JavaScript Questions and Answers
Q1. What is JavaScript and how is it different from HTML and
CSS?
JavaScript is a programming language used to make web pages interactive and
dynamic. HTML is used to create the structure of a web page. CSS is used to style
the web page (colors, fonts, layout).
Difference:
• HTML → Structure (skeleton)
• CSS → Design (look & feel)
• JavaScript → Behavior (actions and interactivity)
Q2. Explain the difference between var, let and const.
• var: Old way to declare variables. It has a wider scope and can be re-declared.
• let: Modern way, block-scoped. Can be updated but not re-declared.
• const: Used to declare constants. Value cannot be changed after assigning.
Example:
var x = 10; // can be re-declared
let y = 20; // can be updated, not re-declared
const z = 30; // fixed value
Q3. What is a data type? List the different data types available in
JavaScript.
Data type tells what kind of value a variable can store.
Different Data Types in JavaScript:
1. Number
2. String
3. Boolean
4. Null
5. Undefined
6. Object
7. Symbol
Q4. What is the purpose of the if statement? Give an example to
explain how it works.
The if statement is used to run a block of code only if a given condition is true.
Example:
let age = 18;
if (age >= 18) {
[Link]("You are eligible to vote");
}
Q5. How does a for loop work? Explain with an example.
A for loop is used to repeat a block of code a fixed number of times.
It has 3 parts:
1. Initialization (start)
2. Condition (check)
3. Increment/Decrement (step)
Example:
for (let i = 1; i <= 5; i++) {
[Link](i);
}
This will print numbers 1 to 5.
Q6. Explain the difference between == and === operators.
== (double equals): Checks only values, not data types.
Example: 5 == "5" → true
=== (triple equals): Checks both values and data types.
Example: 5 === "5" → false