what is defference between console.log(this) in nodejs and console.log(this) in browser?

34 Views Asked by At

When I log (this) in the browser I get window Object, But when I log (this) in node.js I got {}. I road global object in node js is same window object in browser, What is precisely the deference between this and global Object in Node.js? Thanks for your answers.

2

There are 2 best solutions below

0
Zsombor On

Node.js is capable of printing objects without the need to convert them into strings in the console log, while i am guessing in the browser that is not possible. "{}" is an empty object in javascript

0
Robert Rendell On

Simple answer to me seems to be, in both examples this is the global object for the JavaScript runtime.

  • In the browser, this is the Browser Object Model

    • https://www.w3schools.com/js/js_window.asp
    • "All global JavaScript objects, functions, and variables automatically become members of the window object."
    • It is a collection of properties and methods that contain information about the browser and computer screen.
    • Even the document object (of the HTML DOM) is a property of the window object.
  • The difference is in the top-level code in a Node module, this is equivalent to module.exports. That's the empty object you see.