π¨ JavaScript Β· Topic 1 of 20
Introduction & Setup β JavaScript Questions with Answers
30 practice questions on Introduction & Setup, each with the correct answer and a clear explanation of why it is right. Read through them as revision notes, or take this topic as an interactive quiz to test yourself.
15 easy Β· 15 medium Β· 0 hard Β· Part of JavaScript: The language of the web - basics to advanced, 20 topics.
Easy questions (15)
Q1. easy
What is JavaScript mainly used for?
- A Making web pages interactive - and also servers, apps and games
- B Styling web pages only
- C Structuring web page content only
- D Managing databases only
Answer: A β Making web pages interactive - and also servers, apps and games
HTML gives structure, CSS gives style, and JavaScript adds behaviour: reacting to clicks, updating content, fetching data. With Node.js it also runs servers, and it powers mobile and desktop apps too.
- The three web languages: HTML = structure, CSS = looks, JS = behaviour.
HTMLCSSNode.js
Q2. easy
Is JavaScript the same language as Java?
- A No - completely different languages with similar names
- B Yes, JavaScript is a version of Java
- C JavaScript is Java for browsers
- D Java is compiled JavaScript
Answer: A β No - completely different languages with similar names
They are unrelated. JavaScript was named for marketing reasons when Java was popular in 1995. A famous line: 'Java is to JavaScript what car is to carpet'.
History of JavaScript
Q3. easy
Which tag embeds JavaScript inside an HTML page?
<script>
console.log("Hello!");
</script>
<script src="app.js"></script>
- A <script> ... </script>
- B <js> ... </js>
- C <javascript> ... </javascript>
- D <code> ... </code>
Answer: A β <script> ... </script>
The <script> tag holds JavaScript, either written inline or loaded from a file with the src attribute.
script tagHTML
Q4. easy
Which statement prints a message to the browser console?
console.log("Hello");
console.log("Score:", 95);
- A console.log("Hello")
- B print("Hello")
- C echo "Hello"
- D System.out.println("Hello")
Answer: A β console.log("Hello")
console.log() writes to the developer console - the main tool for checking values while coding. print() is Python, echo is PHP, System.out.println is Java.
- Open the console with F12 in most browsers.
consoleDebugging
Q5. easy
Which key usually opens the browser's developer tools?
- A F12
- B F1
- C Esc
- D Tab
Answer: A β F12
F12 (or Ctrl+Shift+I) opens DevTools, where the Console tab shows your console.log output and any errors.
DevToolsConsole
Q6. easy
What file extension do JavaScript files use?
- A .js
- B .java
- C .jsx only
- D .script
Answer: A β .js
JavaScript files end in .js, for example app.js. (.jsx is a React-flavoured variant; .ts is TypeScript.)
Filesscript src
Q7. easy
How do you write a SINGLE-LINE comment in JavaScript?
// a single-line comment
let x = 5; // can follow code too
- A // this is a comment
- B # this is a comment
- C -- this is a comment
- D <!-- comment -->
Answer: A β // this is a comment
// starts a single-line comment; everything after it on that line is ignored. # is Python, -- is SQL, <!-- --> is HTML.
Comments
Q8. easy
How do you write a MULTI-LINE comment?
/*
This explanation
spans several lines.
*/
- A /* comment spanning lines */
- B ''' comment '''
- C // on every line only
- D <!-- comment -->
Answer: A β /* comment spanning lines */
/* ... */ can span any number of lines. It is also used for documentation blocks (/** ... */ is the JSDoc style).
CommentsJSDoc
Q11. easy
Is JavaScript case-sensitive?
- A Yes - myVar, myvar and MyVar are three different names
- B No - capitalization is ignored
- C Only inside functions
- D Only for keywords
Answer: A β Yes - myVar, myvar and MyVar are three different names
Case matters everywhere: getElementById works, GetElementByID does not. Most JS bugs for beginners are simple capitalization mistakes.
IdentifiersNaming
Q13. easy
Which statement shows a popup dialog in the browser?
alert("Welcome!");
- A alert("Hi")
- B console.log("Hi")
- C print("Hi")
- D popup("Hi")
Answer: A β alert("Hi")
alert() opens a blocking popup with an OK button. Handy for quick tests, but it freezes the page - console.log() is far better for real debugging.
alertpromptconfirm
Q16. easy
Which console method displays a message styled as an ERROR (in red)?
console.error("Something broke!");
console.warn("Careful...");
console.table([{a:1},{a:2}]);
- A console.error()
- B console.red()
- C console.warn() only
- D console.fail()
Answer: A β console.error()
console.error() prints in red with a stack trace; console.warn() shows a yellow warning; console.table() prints arrays/objects as a neat table. All are useful for debugging.
console methodsDebugging
Q20. easy
What is the output?
console.log(2 + 3);
console.log("2 + 3");
- A 5 then 2 + 3
- B 5 then 5
- C 2 + 3 then 5
- D Error
Answer: A β 5 then 2 + 3
Without quotes it is a maths expression evaluating to 5. With quotes it is plain text, printed exactly as written.
ExpressionsStrings
Q23. easy
Which is a valid way to link an external JavaScript file?
- A <script src="app.js"></script>
- B <script href="app.js">
- C <link src="app.js">
- D <import file="app.js">
Answer: A β <script src="app.js"></script>
Scripts use the src attribute (href is for <link> and <a>). Note the closing </script> tag is required even when the tag is empty.
script srcHTML
Q25. easy
What is the output?
console.log("A", "B", 1 + 1);
- A A B 2
- B AB2
- C A,B,2
- D A B 1 + 1
Answer: A β A B 2
console.log accepts multiple comma-separated values and prints them separated by spaces, evaluating each expression first: A B 2.
console.log
Q28. easy
Which of these is a popular editor for writing JavaScript?
- A VS Code
- B Notepad (works, but no help)
- C Any text editor technically works
- D All of the above
Answer: D β All of the above
JavaScript is plain text, so any editor works - but VS Code (free) adds syntax highlighting, error hints and autocomplete, which prevents many beginner mistakes.
ToolsVS Code
Medium questions (15)
Q9. medium
Where is the RECOMMENDED place to put a <script> tag that manipulates the page?
<head>
<script src="app.js" defer></script>
</head>
<!-- or -->
<script src="app.js"></script>
</body>
- A Just before </body>, or in <head> with the defer attribute
- B Always at the very top of <head> with no attributes
- C Inside the <title> tag
- D Anywhere - position never matters
Answer: A β Just before </body>, or in <head> with the defer attribute
Scripts run as the browser reaches them. A plain script in <head> runs BEFORE the HTML elements exist, so document.querySelector() finds nothing. Putting it before </body> (or using defer) guarantees the page is ready.
deferasyncDOM ready
Q10. medium
What does the defer attribute do?
<script src="app.js" defer></script>
- A Downloads the script in parallel but runs it only after the HTML is fully parsed
- B Delays the script by 5 seconds
- C Stops the script from ever running
- D Runs the script before the HTML
Answer: A β Downloads the script in parallel but runs it only after the HTML is fully parsed
defer keeps HTML parsing uninterrupted, then executes the script once the document is ready - best of both worlds. async also downloads in parallel but runs as soon as it arrives, in unpredictable order.
defer vs asyncPerformance
Q12. medium
Are semicolons required at the end of statements?
let a = 1;
let b = 2; // explicit semicolons - safest for beginners
- A Not strictly - JavaScript inserts them automatically (ASI) - but writing them is safer and common
- B Yes, always, or the code fails
- C Never allowed
- D Only inside functions
Answer: A β Not strictly - JavaScript inserts them automatically (ASI) - but writing them is safer and common
Automatic Semicolon Insertion adds them for you in most cases, but it occasionally guesses wrong (especially before lines starting with ( or [). Many teams write them; some styles omit them deliberately. Be consistent.
ASICode style
Q14. medium
What does prompt("Your name?") return?
let age = prompt("Age?"); // "25" - a string!
age = Number(age); // 25 - now a number
- A Whatever the user typed, as a STRING (or null if cancelled)
- B A number
- C true or false
- D Nothing
Answer: A β Whatever the user typed, as a STRING (or null if cancelled)
prompt() shows an input dialog and returns the typed text as a string - even if the user types 25. Convert with Number() before doing maths. Cancel returns null.
promptType conversion
Q15. medium
What does confirm("Delete this?") return?
if (confirm("Delete this?")) {
deleteItem();
}
- A true if the user clicks OK, false if Cancel
- B The word "OK"
- C A string
- D Always true
Answer: A β true if the user clicks OK, false if Cancel
confirm() shows an OK/Cancel dialog and returns a boolean - typically used in an if statement to guard a destructive action.
confirmBooleans
Q17. medium
Where can JavaScript run BESIDES the browser?
- A On servers via Node.js - plus desktop (Electron) and mobile (React Native) apps
- B Only in browsers, nowhere else
- C Only on Windows
- D Only inside HTML files
Answer: A β On servers via Node.js - plus desktop (Electron) and mobile (React Native) apps
Node.js runs JavaScript outside the browser, enabling servers, command-line tools and build systems. Electron builds desktop apps (VS Code is one), React Native builds mobile apps.
Node.jsElectronRuntime
Q18. medium
What does 'JavaScript is an interpreted (JIT-compiled) language' mean?
- A The engine reads and runs your source directly - no separate compile step for you
- B You must compile it to .exe first
- C It only runs on interpreters you install
- D It converts to Java bytecode
Answer: A β The engine reads and runs your source directly - no separate compile step for you
You write .js and the browser engine (V8 in Chrome, SpiderMonkey in Firefox) executes it immediately. Modern engines use Just-In-Time compilation internally for speed, but you never run a compiler yourself.
V8 engineJIT
Q19. medium
What is ECMAScript (ES)?
- A The official standard that JavaScript implements - ES6/ES2015 was a major update
- B A JavaScript framework
- C A browser
- D A rival language
Answer: A β The official standard that JavaScript implements - ES6/ES2015 was a major update
ECMAScript is the specification; JavaScript is its best-known implementation. ES6 (2015) introduced let/const, arrow functions, classes, promises and template literals. Yearly updates continue (ES2020, ES2021...).
ES6Standards
Q21. medium
What does "use strict"; at the top of a file do?
"use strict";
x = 5; // ReferenceError instead of a silent global
- A Enables strict mode - catches sloppy mistakes like using undeclared variables
- B Makes the code run faster only
- C Blocks all errors
- D Nothing in modern browsers
Answer: A β Enables strict mode - catches sloppy mistakes like using undeclared variables
Strict mode turns silent errors into thrown errors: assigning to an undeclared variable fails instead of creating a global. ES6 modules and classes are strict automatically.
Strict modeBest practices
Q22. medium
In what order does the browser execute the lines of a script?
- A Top to bottom, one statement at a time (single-threaded)
- B Random order
- C All lines at once in parallel
- D Bottom to top
Answer: A β Top to bottom, one statement at a time (single-threaded)
JavaScript is single-threaded: statements run in order, one at a time. Asynchronous work (timers, network requests) is scheduled and handled later by the event loop - covered in the Async topic.
Single-threadedEvent loop
Q24. medium
Why is keeping JavaScript in a SEPARATE .js file better than writing it inside HTML?
- A Reusable across pages, cached by the browser, and keeps structure separate from behaviour
- B It runs faster in every case
- C Inline JavaScript is not allowed
- D It hides the code from users
Answer: A β Reusable across pages, cached by the browser, and keeps structure separate from behaviour
External files can be shared by many pages, cached after the first download, and edited without touching the HTML. This 'separation of concerns' also keeps files readable. (Note: users can still view the code.)
Separation of concernsCaching
Q26. medium
What happens if a script contains a syntax error?
- A The whole script fails to run, and the error appears in the console
- B Only that line is skipped
- C The browser fixes it automatically
- D The page refuses to load at all
Answer: A β The whole script fails to run, and the error appears in the console
A syntax error stops that script file from executing - the page still displays, but its behaviour is missing. Always check the console (F12) when something 'does nothing'.
SyntaxErrorDebugging
Q27. medium
What does document.write("Hi") do, and why is it discouraged?
- A Writes directly into the page - but it can wipe the whole document if used after loading
- B Writes to a file on disk
- C It is the modern way to update pages
- D It writes to the console
Answer: A β Writes directly into the page - but it can wipe the whole document if used after loading
document.write() injects HTML while the page parses; calling it after load replaces the entire document. Modern code uses DOM methods like textContent or innerHTML instead.
DOMLegacy methods
Q29. medium
What is the browser console REPL useful for?
> 2 + 3
5
> "js".toUpperCase()
'JS'
- A Typing JavaScript directly and seeing results instantly - perfect for experimenting
- B Editing HTML files permanently
- C Installing packages
- D Nothing - it is read-only
Answer: A β Typing JavaScript directly and seeing results instantly - perfect for experimenting
The Console tab lets you run any expression on the current page: check a variable, test a method, inspect an element. Changes are temporary and vanish on refresh.
DevToolsExperimenting
Q30. medium
What is the difference between a statement and an expression?
- A An expression produces a VALUE (2 + 3); a statement performs an ACTION (let x = 5;)
- B They are the same
- C Expressions only appear in loops
- D Statements always return values
Answer: A β An expression produces a VALUE (2 + 3); a statement performs an ACTION (let x = 5;)
Expressions evaluate to a value and can be nested inside other code. Statements are complete instructions. In 'let y = 2 + 3;', the whole line is a statement and '2 + 3' is an expression.
SyntaxEvaluation