1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Introduction to Node.js - Comparing the Browser and Node.js

Introduction to Node.js - Comparing the Browser and Node.js

Contents

keyboard_tab
Introduction to Node.js
Comparing the Browser and Node.js
Overview
Difficulty
Intermediate
Duration
4m
Students
6
Ratings
5/5
starstarstarstarstar
Description

This practical course explores the fundamental difference between the Browser and Node.js.

Learning Objectives

  • Learn how to execute Node in the terminal
  • Learn what happens when the Window object is used in Node.js
  • Learn the differences between the Browser and Node with regards to Web APIs and the DOM

Intended Audience

This course is intended for anyone who wants to learn about Node.js.

Prerequisites

To get the most out of this course, you should have an interest in Node.js in general.

Transcript

Introduction to Node.js -  Comparing the Browser and Node.js. In this video, I'm going to cover the fundamental difference between the Browser and Node.js. I'm going to begin in the Chrome Console, and I will start by typing 'confirm'. Inside of the confirm, parentheses, I will pass through the following confirmation message, "Would you like to continue?"

I will hit 'Enter' to execute. Now on the screen, the confirmation pop-up has appeared. I'm going to hit 'OK'. So, how would this work using Node? I'm going to transition to the Theia lab environment, and I already have an index.js file created. In the file, I will type the same confirm command along with the same message.

Now I'm going to execute the code in this file. In order to do so, I need to type in the terminal, 'node' and the relative path to the file along with the file's name. Since the Terminal is in the same working directory as where the index.js file is located, I just need to type index.js. By using this command, the node runtime environment will read this file, convert the JavaScript code to machine code, and then execute the code. I will go ahead and hit 'Enter' on my keyboard to run the command. Now there is a ReferenceError:  confirm is not defined.

Now to investigate this a little further, I'm going to go back to the Chrome Console, and instead of the console, I will type 'console.log(window)'  and hit 'Enter'. And there is the window object which represents the entirety of the browser. Now I'm going to also console.log(this)  and hit 'Enter'. And this also refers to the window object. If I expand the object, there is the confirm method. I am going to transition back to Theia. 

Now in the Theia lab environment, I'm going to comment out the confirm message. And I'm going to console.log(window). I will clear my terminal and run the node: index.js command again. And now there's a new error message:  window is not defined. To take this further, I'm going to comment out the console.log, and I'm going to console.log(this). And I will clear my terminal again and execute the index.js file. And now in the terminal, there's an empty object. There's some scoping involved. But right now, this references the object node creates for any module exports. Currently, there are no modules being used  hence the empty object. And the discussion about modules is out of scope for this video.

Now going back to when I console.log the window, it came out undefined. Why is that?  Node is a independent JavaScript runtime that runs outside of the browser. Therefore, there is no window. Because there is no window, there is no dumb or Web APIs. The confirm command is a Web API and it will only work within the browser. It doesn't exist in node. Node replaces Web APIs with built in modules that offer different features and functionality. And that's it. Thanks for watching @CloudAcademy.

 

About the Author
Students
2226
Labs
19
Courses
45
Learning Paths
5

Farish has worked in the EdTech industry for over six years. He is passionate about teaching valuable coding skills to help individuals and enterprises succeed.

Previously, Farish worked at 2U Inc in two concurrent roles. Farish worked as an adjunct instructor for 2U’s full-stack boot camps at UCLA and UCR. Farish also worked as a curriculum engineer for multiple full-stack boot camp programs. As a curriculum engineer, Farish’s role was to create activities, projects, and lesson plans taught in the boot camps used by over 50 University partners. Along with these duties, Farish also created nearly 80 videos for the full-stack blended online program.

Before 2U, Farish worked at Codecademy for over four years, both as a content creator and part of the curriculum experience team.

Farish is an avid powerlifter, sushi lover, and occasional Funko collector.