Node.js lets you run JavaScript outside the browser, so you can build things like servers and tools. Browser JavaScript runs inside web pages to make them interactive.
How Node.js differs from browser JavaScript in Node.js
Start learning this pattern below
Jump into concepts and practice - no test required
// Node.js example const fs = require('fs'); fs.readFile('file.txt', 'utf8', (err, data) => { if (err) throw err; console.log(data); }); // Browser JavaScript example fetch('https://api.example.com/data') .then(response => response.json()) .then(data => console.log(data));
Node.js uses modules like fs to access files, which browsers cannot do for security reasons.
Browser JavaScript uses fetch to get data from the internet, which Node.js can also do but often uses other libraries.
// Node.js: Accessing the file system const fs = require('fs'); fs.writeFile('hello.txt', 'Hello from Node.js!', err => { if (err) throw err; console.log('File saved!'); });
<h1> tag on a web page.// Browser JavaScript: Changing page content const heading = document.querySelector('h1'); heading.textContent = 'Hello from the browser!';
global object with info about the environment, unlike browsers.// Node.js: Using global variables console.log(global.process.version);
window object to access browser info.// Browser JavaScript: Using window object console.log(window.navigator.userAgent);
This Node.js program reads a file named example.txt and prints its content. Browsers cannot do this because they don't have file system access.
import fs from 'fs'; // Read a file in Node.js fs.readFile('example.txt', 'utf8', (err, data) => { if (err) { console.error('Error reading file:', err); return; } console.log('File content:', data); });
Node.js can do many things browsers cannot, like reading files or running servers.
Browser JavaScript focuses on interacting with web pages and user actions.
Some JavaScript features work the same in both, but environment objects like window and global differ.
Node.js runs JavaScript outside the browser and can access files and servers.
Browser JavaScript runs inside web pages and controls what users see and do.
They share the JavaScript language but have different tools and objects to work with.
Practice
Solution
Step 1: Understand environment capabilities
Node.js runs outside the browser and can access system resources like files.Step 2: Compare browser limitations
Browser JavaScript runs inside web pages and cannot access the file system for security reasons.Final Answer:
Node.js can access the file system, but browser JavaScript cannot. -> Option DQuick Check:
File system access = Node.js only [OK]
- Thinking browser JavaScript can access local files directly
- Confusing server-side and client-side roles
- Assuming Node.js runs inside web pages
Solution
Step 1: Identify Node.js import syntax
Node.js traditionally uses CommonJS syntax:const fs = require('fs');.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.Final Answer:
const fs = require('fs'); -> Option BQuick Check:
CommonJS require() syntax = const fs = require('fs'); [OK]
- Using browser import syntax without Node.js ES module setup
- Confusing fetch() with module import
- Trying to use import() as a variable assignment
console.log(typeof window);
Solution
Step 1: Understand the 'window' object context
The 'window' object exists in browsers as the global object for the page.Step 2: Check Node.js global objects
Node.js does not have a 'window' object, so it is undefined.Final Answer:
"undefined" -> Option AQuick Check:
window in Node.js = undefined [OK]
- Assuming window exists in Node.js
- Confusing global and window objects
- Expecting 'object' type for window in Node.js
import fs from 'fs';
const data = fs.readFileSync('file.txt', 'utf8');
console.log(data);Solution
Step 1: Check import syntax in Node.js
By default, Node.js uses CommonJS; import requires ES module setup or .mjs extension.Step 2: Validate readFileSync usage
readFileSync is synchronous and does not need a callback.Final Answer:
Using import without enabling ES modules causes a syntax error. -> Option CQuick Check:
import needs ES module setup in Node.js [OK]
- Thinking readFileSync needs a callback
- Assuming relative paths always cause errors
- Believing console.log can't print strings
Solution
Step 1: Understand Node.js capabilities
Node.js can read files using 'fs' and create servers using 'http' or similar modules.Step 2: Identify correct client-server roles
Browser JavaScript cannot read files directly from disk or create servers; Node.js handles server tasks.Final Answer:
Use Node.js to read the file with 'fs', then serve it via an HTTP server module. -> Option AQuick Check:
Node.js reads files and serves content [OK]
- Expecting browser JS to read local files without user action
- Trying to run server code inside the browser
- Confusing client and server responsibilities
