0
0
Node.jsframework~15 mins

How Node.js differs from browser JavaScript in Node.js - Try It Yourself

Choose your learning style9 modes available
Understanding How Node.js Differs from Browser JavaScript
📖 Scenario: You are learning JavaScript and want to understand the differences between running JavaScript in a browser and running it in Node.js on your computer.
🎯 Goal: Create a simple Node.js script that shows how to use a Node.js-specific feature and a browser-specific feature separately, so you can see the difference clearly.
📋 What You'll Learn
Create a variable called message with the value 'Hello from Node.js'
Create a variable called isBrowser that checks if the global window object exists
Use an if statement to print message if running in Node.js (when isBrowser is false)
Add a comment explaining that window is only available in browsers
💡 Why This Matters
🌍 Real World
Developers often need to write JavaScript that works differently in browsers and Node.js environments. Detecting the environment helps run the right code.
💼 Career
Understanding environment differences is key for full-stack developers and anyone working with JavaScript outside the browser.
Progress0 / 4 steps
1
Create a message variable
Create a variable called message and set it to the string 'Hello from Node.js'.
Node.js
Need a hint?

Use const to create a variable that does not change.

2
Check if running in a browser
Create a variable called isBrowser that is true if the global window object exists, otherwise false. Use typeof window !== 'undefined' to check.
Node.js
Need a hint?

Use typeof to safely check if window exists without causing errors.

3
Print message only in Node.js
Write an if statement that prints message only if isBrowser is false (meaning the code runs in Node.js). Use console.log(message) inside the if block.
Node.js
Need a hint?

Use !isBrowser to check if you are not in a browser.

4
Add a comment about window object
Add a comment above the isBrowser variable explaining that the window object is only available in browsers.
Node.js
Need a hint?

Write a simple comment starting with // above the isBrowser line.