Chapter 1 Beginner 55 Questions

Practice Questions — Introduction to JavaScript

← Back to Notes
12 Easy
12 Medium
8 Hard

Topic-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.
number
Question 2
Easy
What is the output?
console.log(typeof "hello");
Anything inside quotes is a string.
string
Question 3
Easy
What is the output?
console.log(10 + 5);
console.log("10" + "5");
The + operator behaves differently with numbers and strings.
15
105
Question 4
Easy
What is the output?
console.log(typeof true);
console.log(typeof false);
true and false are special values in JavaScript.
boolean
boolean
Question 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.
undefined
object
Question 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 old
Question 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 cats
cats 53
Question 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?
undefined
undefined
Question 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?
function
Question 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.
2
53
Question 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?
string
Question 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 old
You 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
-1
1
3
Question 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.
1234
334
Question 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.
20
5

Mixed & Application Questions

Question 1
Easy
What is the output?
console.log("Hello" + " " + "World");
The + operator joins strings together.
Hello World
Question 2
Easy
What is the output?
console.log(10 * 2 + 5);
console.log(10 * (2 + 5));
Multiplication happens before addition, unless you use parentheses.
25
70
Question 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: 96
Question 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.
53
2
15
Question 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?
false
false
true
true
Question 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?
number
false
true
Question 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]);   // object
Question 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.
baNaNa
Question 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?
  • A. Displays a message on the web page
  • B. Prints output to the browser's developer console
  • C. Shows an alert popup
  • D. Writes to a log file on the computer
Answer: B
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')?
  • A. text
  • B. word
  • C. string
  • D. String
Answer: C
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?
  • A. <javascript>code here</javascript>
  • B. <js>code here</js>
  • C. <script>code here</script>
  • D. <code>code here</code>
Answer: C
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')?
  • A. 15
  • B. 105
  • C. NaN
  • D. Error
Answer: B
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?
  • A. null
  • B. undefined
  • C. object
  • D. boolean
Answer: C
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?
  • A. document.findById()
  • B. document.getElementById()
  • C. document.getElement()
  • D. document.selectId()
Answer: B
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)?
  • A. 52
  • B. 3
  • C. NaN
  • D. Error
Answer: B
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