Practice Questions, Introduction to JavaScript
← Back to NotesTopic-Specific Questions
Question 1
Easy
What is the output of the following code?
console.log(typeof 42);typeof returns the type of a value as a string.
numberQuestion 2
Easy
What is the output?
console.log(typeof "hello");Anything inside quotes is a string.
stringQuestion 3
Easy
What is the output?
console.log(10 + 5);
console.log("10" + "5");The + operator behaves differently with numbers and strings.
15105Question 4
Easy
What is the output?
console.log(typeof true);
console.log(typeof false);true and false are special values in JavaScript.
booleanbooleanQuestion 5
Easy
What is the output?
console.log(typeof undefined);
console.log(typeof null);One of these has a famous quirk that is a known bug in JavaScript.
undefinedobjectQuestion 6
Easy
What is the output?
console.log("Aarav" + " is " + 15 + " years old");When you add a string and a number, the number becomes a string.
Aarav is 15 years oldQuestion 7
Medium
What is the output?
console.log(5 + 3 + " cats");
console.log("cats " + 5 + 3);JavaScript evaluates left to right. The + operator switches from addition to concatenation when it encounters a string.
8 catscats 53Question 8
Medium
What is the output?
let x;
console.log(x);
console.log(typeof x);What happens when you declare a variable without assigning a value?
undefinedundefinedQuestion 9
Easy
Write a complete HTML page with a button that shows an alert saying 'Hello, World!' when clicked.
Use the onclick attribute on the button and the alert() function.
<!DOCTYPE html>
<html>
<body>
<button onclick="alert('Hello, World!')">Click Me</button>
</body>
</html>Question 10
Medium
What happens when you run this code?
console.log(typeof console.log);console.log is itself a value. What kind of value is it?
functionQuestion 11
Easy
Rohan wrote this code but gets an error. Find and fix the bug:
<script>
console.log(Hello World);
</script>Text values must be wrapped in something.
<script>
console.log("Hello World");
</script>Question 12
Medium
Ananya wrote this code. The page loads but nothing happens when she clicks the button. Find the bug:
<!DOCTYPE html>
<html>
<head>
<script>
function show() {
document.getElementById("output").textContent = "Clicked!";
}
</script>
</head>
<body>
<p id="output">Waiting...</p>
<button onclick="show()">Click</button>
</body>
</html>Think about when the script runs vs when the button is clicked.
Actually, this code will work! The function is defined in the head, but it does not run until the button is clicked. By the time the user clicks, the
<p> element already exists. The bug is not here. However, the best practice is to move the script before </body>. If the code inside the script tag tried to access the DOM directly (not inside a function), THAT would fail.Question 13
Medium
What is the output?
console.log("5" - 3);
console.log("5" + 3);The - operator does not concatenate strings, but the + operator does.
253Question 14
Hard
What is the output?
console.log(typeof typeof 42);Work from the inside out. What does the inner typeof return? Then what is the typeof of that?
stringQuestion 15
Medium
Write JavaScript code that declares a variable
name with value "Vikram" and a variable age with value 16, then logs: Vikram is 16 years oldYou can combine strings and variables with the + operator.
let name = "Vikram";
let age = 16;
console.log(name + " is " + age + " years old");Question 16
Hard
What is the output?
console.log("" + 1 + 0);
console.log("" - 1 + 0);
console.log(true + false);
console.log("6" / "2");The + operator with strings concatenates. The -, *, / operators always convert to numbers. true = 1, false = 0.
10-113Question 17
Medium
Priya wrote this code to change a heading's text, but nothing changes. Find the bug:
<h1 id="title">Old Title</h1>
<script>
document.getElementById("Title").textContent = "New Title";
</script>JavaScript is case-sensitive when matching IDs.
document.getElementById("title").textContent = "New Title";Question 18
Hard
What is the output?
console.log(1 + "2" + 3 + 4);
console.log(1 + 2 + "3" + 4);JavaScript evaluates left to right. Once a string appears, everything after becomes concatenation.
1234334Question 19
Hard
Write a complete HTML page with an input field and a button. When the button is clicked, read the number from the input and display whether it is positive, negative, or zero in a paragraph below.
Use Number() or parseInt() to convert the input value to a number. Use if-else to check the condition.
<!DOCTYPE html>
<html>
<body>
<input type="number" id="numInput" placeholder="Enter a number">
<button onclick="check()">Check</button>
<p id="result"></p>
<script>
function check() {
let num = Number(document.getElementById("numInput").value);
let result;
if (num > 0) {
result = num + " is positive";
} else if (num < 0) {
result = num + " is negative";
} else {
result = "The number is zero";
}
document.getElementById("result").textContent = result;
}
</script>
</body>
</html>Question 20
Easy
What is the output?
console.log(Math.max(10, 20, 5));
console.log(Math.min(10, 20, 5));Math.max returns the largest number, Math.min returns the smallest.
205Mixed & Application Questions
Question 1
Easy
What is the output?
console.log("Hello" + " " + "World");The + operator joins strings together.
Hello WorldQuestion 2
Easy
What is the output?
console.log(10 * 2 + 5);
console.log(10 * (2 + 5));Multiplication happens before addition, unless you use parentheses.
2570Question 3
Easy
Write JavaScript code to calculate the area of a rectangle with width 12 and height 8. Log the result as: 'Area: 96'
Area = width * height.
let width = 12;
let height = 8;
let area = width * height;
console.log("Area: " + area);
Output: Area: 96Question 4
Medium
What is the output?
let a = "5";
let b = 3;
console.log(a + b);
console.log(a - b);
console.log(a * b);Only the + operator concatenates strings. Other operators convert strings to numbers.
53215Question 5
Medium
What is the output?
console.log(Boolean(0));
console.log(Boolean(""));
console.log(Boolean("0"));
console.log(Boolean(" "));0 and empty string are falsy. But what about the string '0' and a string with just a space?
falsefalsetruetrueQuestion 6
Medium
Write a complete HTML page with a text input and a button. When the button is clicked, convert the text to uppercase and display it in a paragraph below the button.
Use .value to read the input and .toUpperCase() to convert the string.
<!DOCTYPE html>
<html>
<body>
<input type="text" id="textInput" placeholder="Type something">
<button onclick="convert()">Convert</button>
<p id="output"></p>
<script>
function convert() {
let text = document.getElementById("textInput").value;
document.getElementById("output").textContent = text.toUpperCase();
}
</script>
</body>
</html>Question 7
Hard
What is the output?
console.log([] + []);
console.log([] + {});
console.log({} + []);Arrays and objects are converted to strings when used with +. An empty array becomes an empty string.
(empty string)[object Object][object Object]Question 8
Medium
Neha wrote this counter but the number keeps showing as text concatenation instead of addition (clicking +1 shows '01', '011', '0111'). Find and fix the bug:
<p id="count">0</p>
<button onclick="add()">+1</button>
<script>
function add() {
let current = document.getElementById("count").textContent;
document.getElementById("count").textContent = current + 1;
}
</script>What type does .textContent return? How can you convert it to a number?
function add() {
let current = Number(document.getElementById("count").textContent);
document.getElementById("count").textContent = current + 1;
}Question 9
Hard
What is the output?
console.log(typeof NaN);
console.log(NaN === NaN);
console.log(isNaN("hello"));NaN stands for Not a Number, but what type does JavaScript think it is?
numberfalsetrueQuestion 10
Medium
Write JavaScript code that uses typeof to check and log the types of these values: 42, 'Rohan', true, null, undefined, [1,2,3].
Call typeof on each value and log the result.
console.log(typeof 42); // number
console.log(typeof 'Rohan'); // string
console.log(typeof true); // boolean
console.log(typeof null); // object
console.log(typeof undefined); // undefined
console.log(typeof [1, 2, 3]); // objectQuestion 11
Hard
What is the output?
console.log("b" + "a" + + "a" + "a");Pay attention to the + + part. The second + is a unary plus operator trying to convert 'a' to a number.
baNaNaQuestion 12
Hard
Write a complete HTML page with two number inputs and four buttons (+, -, *, /). When a button is clicked, perform the operation on the two numbers and display the result.
Use Number() to convert input values. Create a function for each operation or one function that takes the operator.
<!DOCTYPE html>
<html>
<body>
<input type="number" id="num1" placeholder="Number 1">
<input type="number" id="num2" placeholder="Number 2"><br><br>
<button onclick="calc('+')">+</button>
<button onclick="calc('-')">-</button>
<button onclick="calc('*')">*</button>
<button onclick="calc('/')">/</button>
<p id="result"></p>
<script>
function calc(op) {
let a = Number(document.getElementById("num1").value);
let b = Number(document.getElementById("num2").value);
let result;
if (op === '+') result = a + b;
else if (op === '-') result = a - b;
else if (op === '*') result = a * b;
else if (op === '/') result = a / b;
document.getElementById("result").textContent = "Result: " + result;
}
</script>
</body>
</html>Multiple Choice Questions
MCQ 1
What does console.log() do in JavaScript?
Answer: B
B is correct.
B is correct.
console.log() prints output to the browser's Developer Console (accessible via F12). It does NOT display anything on the web page (A), for that you would use textContent or innerHTML. It does not show a popup (C). That is alert(). It does not write files (D).MCQ 2
What is the output of: console.log(typeof 'hello')?
Answer: C
C is correct.
C is correct.
typeof 'hello' returns "string" (lowercase). JavaScript's typeof returns lowercase type names. String with a capital S (D) is the String constructor, not the typeof result.MCQ 3
Which of these correctly places JavaScript in an HTML file?
Answer: C
C is correct. JavaScript is placed inside
C is correct. JavaScript is placed inside
<script> tags in HTML. There are no <javascript> (A) or <js> (B) tags. The <code> tag (D) is for displaying code as text, not executing it.MCQ 4
What is the output of: console.log(10 + '5')?
Answer: B
B is correct. When the
B is correct. When the
+ operator is used with a number and a string, JavaScript converts the number to a string and performs concatenation. 10 + '5' becomes '10' + '5' = '105'. It does not perform addition (A).MCQ 5
What does typeof null return in JavaScript?
Answer: C
C is correct.
C is correct.
typeof null returns "object". This is a well-known bug from the first version of JavaScript in 1995. Logically it should return "null", but the bug was never fixed to avoid breaking existing code.MCQ 6
Which method finds an HTML element by its id?
Answer: B
B is correct.
B is correct.
document.getElementById() is the standard method to find an HTML element by its id attribute. The other options are not valid JavaScript methods.MCQ 7
What is the output of: console.log('5' - 2)?
Answer: B
B is correct. The
B is correct. The
- operator does not do string concatenation. It only works with numbers. JavaScript converts '5' to the number 5 and performs subtraction: 5 - 2 = 3. This is different from + which would give '52'.MCQ 8
Where should you place the
<script> tag for best practice?Answer: C
C is correct. Placing the script just before
C is correct. Placing the script just before
</body> ensures all HTML elements have been created before the JavaScript runs. Placing it in the head (B) can cause errors if the script tries to access elements that do not exist yet. After </html> (D) is invalid.MCQ 9
What is the output of: console.log(typeof typeof 42)?
Answer: B
B is correct. The inner
B is correct. The inner
typeof 42 returns the string "number". Then typeof "number" returns "string", because "number" is a string value. typeof always returns a string.MCQ 10
What does prompt() return if the user clicks Cancel?
Answer: C
C is correct. When the user clicks Cancel on a
C is correct. When the user clicks Cancel on a
prompt() dialog, the function returns null. If the user clicks OK without typing anything, it returns an empty string "". This distinction matters when processing user input.MCQ 11
What is the output of: console.log(5 + 3 + '2')?
Answer: C
C is correct. JavaScript evaluates left to right.
C is correct. JavaScript evaluates left to right.
5 + 3 = 8 (both numbers, so addition). Then 8 + '2' = '82' (number + string, so concatenation). The string '2' triggers concatenation only from that point onward.MCQ 12
Which of the following is NOT a valid way to declare a variable in JavaScript?
Answer: D
D is correct. JavaScript does not have an
D is correct. JavaScript does not have an
int keyword. That is from languages like C and Java. In JavaScript, you declare variables with let, const, or var. JavaScript variables do not have explicit types.MCQ 13
What is the output of: console.log('b' + 'a' + + 'a' + 'a')?
Answer: B
B is correct.
B is correct.
'b' + 'a' gives 'ba'. The + 'a' is a unary plus on the string 'a', which cannot be converted to a number, so it becomes NaN. Then 'ba' + NaN gives 'baNaN'. Finally 'baNaN' + 'a' gives 'baNaNa'.MCQ 14
What is the output of: console.log('' == false)?
Answer: A
A is correct. The
A is correct. The
== operator performs type coercion. An empty string '' is coerced to 0, and false is also coerced to 0. 0 == 0 is true. This is why === (strict equality without coercion) is recommended over ==.MCQ 15
What is the output of: console.log(NaN === NaN)?
Answer: B
B is correct.
B is correct.
NaN is the only value in JavaScript that is not equal to itself. NaN === NaN is false. To check if a value is NaN, use Number.isNaN() or isNaN().MCQ 16
What is the output of: console.log('' + 1 + 0)?
Answer: B
B is correct.
B is correct.
'' + 1 is string concatenation, giving '1'. Then '1' + 0 is also string concatenation, giving '10'. An empty string plus a number always gives a string, not a number.MCQ 17
Which statement about JavaScript is TRUE?
Answer: C
C is correct.
C is correct.
typeof null returning 'object' is a well-documented bug from 1995. JavaScript and Java are different languages (A). JavaScript also runs in Node.js outside browsers (B). JavaScript was created in 1995, not 2005 (D).MCQ 18
What is the output of: console.log(2 + 2)?
Answer: B
B is correct. Both
B is correct. Both
2 and 2 are numbers, so + performs addition: 2 + 2 = 4. It would only be "22" (A) if one of them were a string like '2' + 2.MCQ 19
What does the confirm() function return when the user clicks OK?
Answer: C
C is correct.
C is correct.
confirm() shows a dialog with OK and Cancel buttons. Clicking OK returns the boolean true. Clicking Cancel returns false. It does not return a number or a string.MCQ 20
What is the output of: console.log(true + true + true)?
Answer: B
B is correct. When used with the
B is correct. When used with the
+ operator and no strings are involved, true is converted to 1 and false to 0. So true + true + true = 1 + 1 + 1 = 3.Coding Challenges
Challenge 1: Personal Introduction Page
EasyCreate an HTML page that displays your name, age, and favorite subject. Use JavaScript to set all the text content using getElementById (do not hardcode the text in HTML).
Sample Input
(No input required)
Sample Output
Name: Aarav
Age: 15
Favorite Subject: Computer Science
HTML should have empty paragraphs with IDs. JavaScript should fill in all the text.
<!DOCTYPE html>
<html>
<body>
<p id="name"></p>
<p id="age"></p>
<p id="subject"></p>
<script>
document.getElementById("name").textContent = "Name: Aarav";
document.getElementById("age").textContent = "Age: 15";
document.getElementById("subject").textContent = "Favorite Subject: Computer Science";
</script>
</body>
</html>Challenge 2: Background Color Picker
EasyCreate an HTML page with three buttons: Red, Green, and Blue. Clicking a button changes the page's background color to that color using JavaScript.
Sample Input
User clicks the Green button
Sample Output
The entire page background turns green.
Use document.body.style.backgroundColor to change the color.
<!DOCTYPE html>
<html>
<body>
<h1>Pick a Background Color</h1>
<button onclick="document.body.style.backgroundColor='red'">Red</button>
<button onclick="document.body.style.backgroundColor='green'">Green</button>
<button onclick="document.body.style.backgroundColor='blue'">Blue</button>
</body>
</html>Challenge 3: Character Counter
MediumCreate an HTML page with a text input and a paragraph below it. As the user types (use the oninput event), show the number of characters typed in real time. For example: 'Characters: 7'
Sample Input
User types 'Priya' in the input
Sample Output
Characters: 5
Use the oninput event and the .length property of the string.
<!DOCTYPE html>
<html>
<body>
<input type="text" id="textInput" oninput="countChars()" placeholder="Start typing...">
<p id="counter">Characters: 0</p>
<script>
function countChars() {
let text = document.getElementById("textInput").value;
document.getElementById("counter").textContent = "Characters: " + text.length;
}
</script>
</body>
</html>Challenge 4: Temperature Converter
MediumBuild an HTML page with a number input for Celsius and a button. When clicked, convert the Celsius value to Fahrenheit using the formula F = (C * 9/5) + 32 and display the result. Also display whether it is 'Hot' (above 35C), 'Comfortable' (15-35C), or 'Cold' (below 15C).
Sample Input
User enters 40
Sample Output
40 C = 104 F - Hot!
Use Number() to convert input. Use if-else for the temperature category.
<!DOCTYPE html>
<html>
<body>
<input type="number" id="celsius" placeholder="Celsius">
<button onclick="convert()">Convert</button>
<p id="result"></p>
<script>
function convert() {
let c = Number(document.getElementById("celsius").value);
let f = (c * 9/5) + 32;
let category;
if (c > 35) category = "Hot!";
else if (c >= 15) category = "Comfortable";
else category = "Cold!";
document.getElementById("result").textContent = c + " C = " + f + " F - " + category;
}
</script>
</body>
</html>Challenge 5: Show/Hide Toggle
MediumCreate an HTML page with a paragraph of text and a button that says 'Hide'. When clicked, the paragraph disappears and the button text changes to 'Show'. Clicking again makes the paragraph reappear and the button says 'Hide'.
Sample Input
User clicks the button
Sample Output
The paragraph toggles between visible and hidden.
Use style.display = 'none' and style.display = 'block' to toggle visibility.
<!DOCTYPE html>
<html>
<body>
<p id="text">This paragraph can be toggled. Try clicking the button below!</p>
<button id="btn" onclick="toggle()">Hide</button>
<script>
function toggle() {
let text = document.getElementById("text");
let btn = document.getElementById("btn");
if (text.style.display === "none") {
text.style.display = "block";
btn.textContent = "Hide";
} else {
text.style.display = "none";
btn.textContent = "Show";
}
}
</script>
</body>
</html>Challenge 6: Type Checker Tool
HardBuild a page with an input field and a button. When the user enters a value and clicks the button, determine and display whether the input is: a number, the word 'true' or 'false' (boolean-like), an empty string, or just a regular string. Display the typeof the original input and the converted type.
Sample Input
User enters '42'
Sample Output
You entered: '42'
typeof (raw input): string
Detected type: number
Converted value: 42
Input values are always strings. Use Number(), isNaN(), and conditional checks.
<!DOCTYPE html>
<html>
<body>
<input type="text" id="val" placeholder="Enter any value">
<button onclick="checkType()">Check Type</button>
<p id="output"></p>
<script>
function checkType() {
let raw = document.getElementById("val").value;
let result = "You entered: '" + raw + "'\n";
result += "typeof (raw input): " + typeof raw + "\n";
if (raw === "") {
result += "Detected type: empty string";
} else if (raw === "true" || raw === "false") {
result += "Detected type: boolean-like\nConverted value: " + (raw === "true");
} else if (!isNaN(Number(raw))) {
result += "Detected type: number\nConverted value: " + Number(raw);
} else {
result += "Detected type: regular string";
}
document.getElementById("output").textContent = result;
}
</script>
</body>
</html>Challenge 7: Build a Simple Quiz Question
HardCreate a page with a question: 'What is typeof null in JavaScript?' with four clickable buttons (A: null, B: undefined, C: object, D: boolean). When the user clicks the correct answer (C), show 'Correct!' in green. For wrong answers, show 'Wrong! The answer is C: object' in red.
Sample Input
User clicks option C
Sample Output
Correct! (displayed in green)
Use style.color to change text color. Use if-else to check the answer.
<!DOCTYPE html>
<html>
<body>
<h2>What is typeof null in JavaScript?</h2>
<button onclick="check('A')">A: null</button>
<button onclick="check('B')">B: undefined</button>
<button onclick="check('C')">C: object</button>
<button onclick="check('D')">D: boolean</button>
<p id="feedback"></p>
<script>
function check(answer) {
let fb = document.getElementById("feedback");
if (answer === "C") {
fb.textContent = "Correct!";
fb.style.color = "green";
} else {
fb.textContent = "Wrong! The answer is C: object";
fb.style.color = "red";
}
}
</script>
</body>
</html>Challenge 8: Dynamic Greeting Based on Time
HardCreate a page that automatically shows a greeting based on the current time: 'Good Morning' (5am-12pm), 'Good Afternoon' (12pm-5pm), 'Good Evening' (5pm-9pm), 'Good Night' (9pm-5am). Use JavaScript's Date object (new Date().getHours()) to get the current hour. Also display the current time.
Sample Input
(No input - uses current time automatically)
Sample Output
Good Afternoon! It is 2:30 PM.
Use new Date().getHours() and if-else to determine the greeting.
<!DOCTYPE html>
<html>
<body>
<h1 id="greeting"></h1>
<p id="time"></p>
<script>
let now = new Date();
let hour = now.getHours();
let greeting;
if (hour >= 5 && hour < 12) greeting = "Good Morning!";
else if (hour >= 12 && hour < 17) greeting = "Good Afternoon!";
else if (hour >= 17 && hour < 21) greeting = "Good Evening!";
else greeting = "Good Night!";
document.getElementById("greeting").textContent = greeting;
document.getElementById("time").textContent = "Current time: " + now.toLocaleTimeString();
</script>
</body>
</html>Need to Review the Concepts?
Go back to the detailed notes for this chapter.
Read Chapter Notes
Learn it live
Want a teacher to take you further than Introduction to JavaScript?
Notes explain the idea. A live teacher checks how you think, spots the exact step that slips, and moves at your pace, in a small group or 1-on-1, at a time that suits your time zone.