Bird
Raised Fist0
Node.jsframework~20 mins

How Node.js differs from browser JavaScript in Node.js - Practice Exercises

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Node.js vs Browser JavaScript Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Understanding Global Objects in Node.js vs Browser
Which global object is available in Node.js but NOT in browser JavaScript?
Aglobal
Bnavigator
Cwindow
Ddocument
Attempts:
2 left
💡 Hint
Think about the object that represents the global scope in Node.js.
component_behavior
intermediate
1:30remaining
File System Access in Node.js vs Browser
What happens if you try to use Node.js's 'fs' module in browser JavaScript?
AIt throws a ReferenceError because 'fs' is not defined
BIt works normally and reads files from the user's computer
CIt reads files but only from the browser cache
DIt prompts the user to select files manually
Attempts:
2 left
💡 Hint
Consider what modules are available in browsers by default.
📝 Syntax
advanced
2:00remaining
Using ES Modules in Node.js vs Browser
Which statement about ES Modules (import/export) in Node.js and browsers is correct?
ABrowsers do not support ES Modules natively
BBrowsers require .mjs extension for ES Modules to work
CNode.js does not support ES Modules at all
DNode.js supports ES Modules natively only if files have .mjs extension or 'type':'module' in package.json
Attempts:
2 left
💡 Hint
Think about how Node.js handles module types compared to browsers.
state_output
advanced
2:00remaining
Event Loop Behavior in Node.js vs Browser
What is a key difference in how the event loop handles timers in Node.js compared to browsers?
ABrowsers queue timers in the microtask queue, Node.js does not
BBrowsers execute timers immediately without delay, Node.js always delays
CNode.js uses separate phases for timers and I/O callbacks, browsers do not
DNode.js does not support setTimeout, browsers do
Attempts:
2 left
💡 Hint
Consider the phases of the Node.js event loop.
🔧 Debug
expert
2:30remaining
Identifying Error When Using Browser API in Node.js
What error will occur if you run this Node.js code? const element = document.getElementById('app'); console.log(element);
ATypeError: getElementById is not a function
BReferenceError: document is not defined
CSyntaxError: Unexpected identifier
DNo error, logs null
Attempts:
2 left
💡 Hint
Think about what 'document' means in Node.js.

Practice

(1/5)
1. Which of the following is a key difference between Node.js and browser JavaScript?
easy
A. Browser JavaScript can run server-side code, but Node.js cannot.
B. Browser JavaScript can access databases directly, but Node.js cannot.
C. Node.js runs only inside web pages, while browser JavaScript runs on servers.
D. Node.js can access the file system, but browser JavaScript cannot.

Solution

  1. Step 1: Understand environment capabilities

    Node.js runs outside the browser and can access system resources like files.
  2. Step 2: Compare browser limitations

    Browser JavaScript runs inside web pages and cannot access the file system for security reasons.
  3. Final Answer:

    Node.js can access the file system, but browser JavaScript cannot. -> Option D
  4. Quick Check:

    File system access = Node.js only [OK]
Hint: Remember: Node.js can read files; browsers cannot [OK]
Common Mistakes:
  • Thinking browser JavaScript can access local files directly
  • Confusing server-side and client-side roles
  • Assuming Node.js runs inside web pages
2. Which syntax correctly imports the built-in 'fs' module in Node.js?
easy
A. import fs from 'fs';
B. const fs = require('fs');
C. let fs = import('fs');
D. var fs = fetch('fs');

Solution

  1. Step 1: Identify Node.js import syntax

    Node.js traditionally uses CommonJS syntax: const fs = require('fs');.
  2. Step 2: Check other options

    import fs from 'fs'; is ES module syntax, which requires special setup; var fs = fetch('fs'); and let fs = import('fs'); are invalid for module import.
  3. Final Answer:

    const fs = require('fs'); -> Option B
  4. Quick Check:

    CommonJS require() syntax = const fs = require('fs'); [OK]
Hint: Use require() to import built-in modules in Node.js [OK]
Common Mistakes:
  • Using browser import syntax without Node.js ES module setup
  • Confusing fetch() with module import
  • Trying to use import() as a variable assignment
3. What will the following Node.js code output?
console.log(typeof window);
medium
A. "undefined"
B. "object"
C. "function"
D. "null"

Solution

  1. Step 1: Understand the 'window' object context

    The 'window' object exists in browsers as the global object for the page.
  2. Step 2: Check Node.js global objects

    Node.js does not have a 'window' object, so it is undefined.
  3. Final Answer:

    "undefined" -> Option A
  4. Quick Check:

    window in Node.js = undefined [OK]
Hint: window is browser-only; Node.js has no window [OK]
Common Mistakes:
  • Assuming window exists in Node.js
  • Confusing global and window objects
  • Expecting 'object' type for window in Node.js
4. Identify the error in this Node.js code snippet:
import fs from 'fs';
const data = fs.readFileSync('file.txt', 'utf8');
console.log(data);
medium
A. The file path must be absolute, not relative.
B. readFileSync requires a callback function.
C. Using import without enabling ES modules causes a syntax error.
D. console.log cannot print file contents.

Solution

  1. Step 1: Check import syntax in Node.js

    By default, Node.js uses CommonJS; import requires ES module setup or .mjs extension.
  2. Step 2: Validate readFileSync usage

    readFileSync is synchronous and does not need a callback.
  3. Final Answer:

    Using import without enabling ES modules causes a syntax error. -> Option C
  4. Quick Check:

    import needs ES module setup in Node.js [OK]
Hint: Use require() or enable ES modules for import [OK]
Common Mistakes:
  • Thinking readFileSync needs a callback
  • Assuming relative paths always cause errors
  • Believing console.log can't print strings
5. You want to write a Node.js script that reads a file and sends its content to a web page. Which approach correctly uses Node.js features to do this?
hard
A. Use Node.js to read the file with 'fs', then serve it via an HTTP server module.
B. Use browser JavaScript to read the file directly and send it to the server.
C. Use Node.js to run code inside the browser to access the file system.
D. Use browser JavaScript to create an HTTP server and read files.

Solution

  1. Step 1: Understand Node.js capabilities

    Node.js can read files using 'fs' and create servers using 'http' or similar modules.
  2. Step 2: Identify correct client-server roles

    Browser JavaScript cannot read files directly from disk or create servers; Node.js handles server tasks.
  3. Final Answer:

    Use Node.js to read the file with 'fs', then serve it via an HTTP server module. -> Option A
  4. Quick Check:

    Node.js reads files and serves content [OK]
Hint: Node.js handles files and servers; browsers handle UI [OK]
Common Mistakes:
  • Expecting browser JS to read local files without user action
  • Trying to run server code inside the browser
  • Confusing client and server responsibilities