Quick Contact


    Global Object in Node JS

    When you write JavaScript in browser the global object is the window object and you could access properties and methods on that window object such as alert scroll or set timeout etc. When you write JavaScript in node the global object is no longer the window object. The global object in node is an object called global but much like the window object it gives us access to some methods you can use straight out of the box in node JS.

    Global object in Node JS can use in different methods like set interval, set timeout and console which were also available on the window object in the browser and they are also available on the global object in node JS.

    So let’s try-:
    As we have app.JS file
    First write any message under console.log as-:

    Console.log (‘hey dear’);

    And run in terminal by writing “node app”

    And that’s going to log the message to console so let’s try on window objects.
    	setTimeout (function(){
                        console.log (‘3 seconds have passed’)
                                }, 3000
                                  );
    	

    So, when you run this file in node set timeout is going to fire this function after 3000 milliseconds which are three seconds and then it’s going to log this message to the terminal.

    Now again write “node app” in terminal and press Enter

    And you will get “3 seconds have passed” in output after 3 seconds. Now you can also set “interval” tools on the global object again much like on the window object and this time going to change to 2000 milliseconds interval and give pop up using variable.

    Create a variable var time = 0;

    So every time this function is called as its going to set this to set interval not set timeout so every time this function is called after 2000 milliseconds, add two to this time variable as-:
    	      Time += 2
    	Var time = 0;
    	setInterval(function(){
    	   Time += 2;
    	   Console.log (time ‘3 seconds have passed’);
    	 };
    	

    Then you are going to log here time to

    	Var time = 0;
    	setInterval (function){
    	         Time += 2;
    	       console.log (time + ‘seconds have passed’);
    		     ), 2000);
    	
    Here how many seconds have passed comes under console.log
    Now save the above file and run it in terminal by writing “Node app”
    Output is-: every two seconds you are going to see a message logged to the console and its updating each time around and further you will notice node will still running this file. As it caught in some kind of loop if you like and it’s never going to end unless you tell it to end. So, way to end the process in the terminal is by pressing CNTRL+C.
    You can also use clay interval and to do that you will set this interval equal to variable, known it as timer. You can also check it easily if the time is over five seconds then what you have to do is clear the interval and the interval is this thing right here the timer so let’s pop that in timer as-:
    	             If (time > 5) {
    	clearInterval(timer);
    	}
    	So file is as-:
    	Var time = 0;
    	Var timer = setInterval(function){
    	       Time <= 2;
    	     Console.log (time + ‘seconds have passed’);
    	if (time > 5){
    	clearInterval(timer);
    	    }
    	}, 2000);
    	

    Now run it in Terminal by writing “node app”

    And output is-

    Again you have to wait for 2 seconds and print out messages to terminal like “2 seconds have passed” “4 seconds have passed”

    Then after it reaches to the 6 seconds one because it’s over five seconds then it’s going to clear that interval.

    So Node can tell where you are in a particular directory or what fire you are in and it will be done as when you write something in console log as-:

    Console.log (__dirname);

    Here directory name can be used wherever there is new application so, if you run the file by writing the console then

    Output is- terminal tells the directory that you are in and its node JS playlist and that’s directory that this file is in you can see it sits in the node JS playlist direction.

    If you write filename in console log as-:

    Console.log (__dirname);

    Console.log (__filename);

    Save and run in terminal by writing again node app then it gives full file as the directory.

    So there are two utilities that you can use again anywhere in node JS or anywhere in application in Node JS because they sit on the global object.

    You can focus on more global objects like “require”, “exports” etc.

    Copyright 1999- Ducat Creative, All rights reserved.