πŸŽ“ Velnov

🟨 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.

Key concepts in this topic

HTMLCSSNode.jsHistory of JavaScriptscript tagconsoleDebuggingDevToolsConsoleFilesscript srcCommentsJSDocdeferasyncDOM readydefer vs asyncPerformanceIdentifiersNamingASICode stylealertprompt

Jump to: Easy Β· Medium

Easy questions (15)

Q1. easy

What is JavaScript mainly used for?

  1. A Making web pages interactive - and also servers, apps and games
  2. B Styling web pages only
  3. C Structuring web page content only
  4. 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?

  1. A No - completely different languages with similar names
  2. B Yes, JavaScript is a version of Java
  3. C JavaScript is Java for browsers
  4. 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>
  1. A <script> ... </script>
  2. B <js> ... </js>
  3. C <javascript> ... </javascript>
  4. 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);
  1. A console.log("Hello")
  2. B print("Hello")
  3. C echo "Hello"
  4. 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?

  1. A F12
  2. B F1
  3. C Esc
  4. 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?

  1. A .js
  2. B .java
  3. C .jsx only
  4. 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
  1. A // this is a comment
  2. B # this is a comment
  3. C -- this is a comment
  4. 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.
*/
  1. A /* comment spanning lines */
  2. B ''' comment '''
  3. C // on every line only
  4. 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?

  1. A Yes - myVar, myvar and MyVar are three different names
  2. B No - capitalization is ignored
  3. C Only inside functions
  4. 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!");
  1. A alert("Hi")
  2. B console.log("Hi")
  3. C print("Hi")
  4. 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}]);
  1. A console.error()
  2. B console.red()
  3. C console.warn() only
  4. 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");

  1. A 5 then 2 + 3
  2. B 5 then 5
  3. C 2 + 3 then 5
  4. 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?

  1. A <script src="app.js"></script>
  2. B <script href="app.js">
  3. C <link src="app.js">
  4. 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);

  1. A A B 2
  2. B AB2
  3. C A,B,2
  4. 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?

  1. A VS Code
  2. B Notepad (works, but no help)
  3. C Any text editor technically works
  4. 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>
  1. A Just before </body>, or in <head> with the defer attribute
  2. B Always at the very top of <head> with no attributes
  3. C Inside the <title> tag
  4. 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>

  1. A Downloads the script in parallel but runs it only after the HTML is fully parsed
  2. B Delays the script by 5 seconds
  3. C Stops the script from ever running
  4. 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
  1. A Not strictly - JavaScript inserts them automatically (ASI) - but writing them is safer and common
  2. B Yes, always, or the code fails
  3. C Never allowed
  4. 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
  1. A Whatever the user typed, as a STRING (or null if cancelled)
  2. B A number
  3. C true or false
  4. 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();
}
  1. A true if the user clicks OK, false if Cancel
  2. B The word "OK"
  3. C A string
  4. 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?

  1. A On servers via Node.js - plus desktop (Electron) and mobile (React Native) apps
  2. B Only in browsers, nowhere else
  3. C Only on Windows
  4. 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?

  1. A The engine reads and runs your source directly - no separate compile step for you
  2. B You must compile it to .exe first
  3. C It only runs on interpreters you install
  4. 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)?

  1. A The official standard that JavaScript implements - ES6/ES2015 was a major update
  2. B A JavaScript framework
  3. C A browser
  4. 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
  1. A Enables strict mode - catches sloppy mistakes like using undeclared variables
  2. B Makes the code run faster only
  3. C Blocks all errors
  4. 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?

  1. A Top to bottom, one statement at a time (single-threaded)
  2. B Random order
  3. C All lines at once in parallel
  4. 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?

  1. A Reusable across pages, cached by the browser, and keeps structure separate from behaviour
  2. B It runs faster in every case
  3. C Inline JavaScript is not allowed
  4. 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?

  1. A The whole script fails to run, and the error appears in the console
  2. B Only that line is skipped
  3. C The browser fixes it automatically
  4. 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?

  1. A Writes directly into the page - but it can wipe the whole document if used after loading
  2. B Writes to a file on disk
  3. C It is the modern way to update pages
  4. 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'
  1. A Typing JavaScript directly and seeing results instantly - perfect for experimenting
  2. B Editing HTML files permanently
  3. C Installing packages
  4. 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?

  1. A An expression produces a VALUE (2 + 3); a statement performs an ACTION (let x = 5;)
  2. B They are the same
  3. C Expressions only appear in loops
  4. 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

Practice β€œIntroduction & Setup” as a quiz