How it feels to learn JavaScript
  • How it feels to learn JS
  • Table of Contents
  • Introduction
    • Is EcmaScript is JavaScript? TC39
  • JS Basics
    • Basic Concepts
    • Objects
    • Function vs Method
      • Anonymous Function
    • Scopes and keyword "this"
    • Prototypical
  • Dependencies / Packages Management
    • Chapter: Dependencies / Package Management
    • NPM
    • Bower
  • JS-Engines
    • Browser vs Node.js
  • Super / Sub - Lang
    • Chapter: Super / Sub Languages
    • TypeScript
      • Architectural Overview
  • Framework / Library
    • Angular
      • Bootstrapping
      • Change Detection
      • Singleton Services
    • React
  • Paradigms
    • Reactive
      • Rx.js
  • Web API's
    • Web Sockets
    • Web Worker
    • Service Worker
  • Testing
    • Testing Essentials
  • Patterns
    • IIFE (Immediately Invoked Function Expression)
Powered by GitBook
On this page
  1. JS-Engines

Browser vs Node.js

PreviousBowerNextChapter: Super / Sub Languages

Last updated 6 years ago

Is same code can be executed in chrome javascript engine as well as in nodejs since both are based on v8 engine. Well yes & no, I am not trying to create confusing here, but although both are based out of v8 engine. Still there is a difference between the ways of handling javascript code.

One resides inside your browser while other runs directly over os layer. In browser everything revoles around document object model (dom) while inside nodejs echo system, we don't have anything like dom.

To verify this open your browser javascript console and in seperate terminal type node. Type document in the chrome console.

Open a terminal and type node.

> document
ReferenceError: document is not defined
    at repl:1:1
    at ContextifyScript.Script.runInThisContext (vm.js:23:33)
    at REPLServer.defaultEval (repl.js:336:29)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)
    at REPLServer.onLine (repl.js:533:10)
    at emitOne (events.js:101:20)
    at REPLServer.emit (events.js:191:7)
    at REPLServer.Interface._onLine (readline.js:238:10)
    at REPLServer.Interface._line (readline.js:582:8)
> window
ReferenceError: window is not defined
    at repl:1:1
    at ContextifyScript.Script.runInThisContext (vm.js:23:33)
    at REPLServer.defaultEval (repl.js:336:29)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)
    at REPLServer.onLine (repl.js:533:10)
    at emitOne (events.js:101:20)
    at REPLServer.emit (events.js:191:7)
    at REPLServer.Interface._onLine (readline.js:238:10)
    at REPLServer.Interface._line (readline.js:582:8)

You can see the difference in browser and node js console. document and window keywords are not defined inside node.js echo system while in browser, they provide you details about each in details. if you observe closely window keyword expose all the api's that can be used to call or modify specific element in the browser.