4

For me variables are easy to understand in Javascript: if a variable is not in local scope then it is a field in the global object.

But what about Javascript commands? If i just write Javascript commands in a file (outside any function) then how will the Javascript engine interpret it?

----- file.js -----
console.log('hai:DDD');
--- end of file ---

Will it create some kind of "global" or "main" function type object with the commands and then execute it? What if there are multiple files with code?

I guess this question only applies to node.js because in browsers all Javascript code is event handlers

Kaarel Purde
  • 1,255
  • 4
  • 18
  • 38
  • There's no concept of a main entry point in JS, but you should define one in your code. – plalx Feb 29 '16 at 18:33

5 Answers5

6

Javascript does not have a main function. It starts at the top and works it's way down to the bottom.

In Node.js, variables are stored in the module scope which basically means they're not quite global. In a way, you could imagine any code you run in Node.js as being wrapped up like this:

(function(exports, require, module, __filename, __dirname) {
   ...
})();

But you seem to have a misconception about the browser. Not all JS code is an event handler in the browser. If you just run a basic script in the browser it will also populate the global scope.

var myGlobal = "I'm available to everyone";
Community
  • 1
  • 1
Mike Cluck
  • 31,869
  • 13
  • 80
  • 91
3

Javascript is, as the name implies, a scripting language to be interpreted by some Javascript interpreter. Thus, the "main function" can be thought of as the whole file, the entry point is at the first character of the first line in the script. Typically, the entirety of the functions the script is to perform are wrapped in a function that loads with the page, but this isn't necessary, just convenient.

Mike Cluck
  • 31,869
  • 13
  • 80
  • 91
ocket8888
  • 1,060
  • 12
  • 31
1

There is no global function in JavaScript, but there are some similar concepts:

  • The Global Environment (10.2.3)

    The global environment is a unique Lexical Environment which is created before any ECMAScript code is executed. The global environment’s Environment Record is an object environment record whose binding object is the global object (15.1). The global environment’s outer environment reference is null.

  • The Global Object (15.1)

    An unique object used as the binding of the environment record of the global environment.

  • Global Code (10.4.1, 10.1)

    Global code is source text that is treated as an ECMAScript Program. The global code of a particular Program does not include any source text that is parsed as part of a FunctionBody.

  • Global Execution Context (10.4.1.1)

    An execution context of global code.

Oriol
  • 274,082
  • 63
  • 437
  • 513
0

No, javascript is a scripting language, there is no point of insertion.

Lines of code are executed in the order they are encountered by the javascript interpreter.

If multiple files are included on the page, the functions and variables declared in them will be added to the global scope (unless they are declared in an anonymous function)

glcheetham
  • 973
  • 8
  • 23
0

Here is a link to video.. watch this he explains how javascript works. link to the video

And tool to visualize how JavaScript works. link to the tool

In case if you want to run after the window is loaded there is window.onload , $(document).ready(); if you are using Jquery.

  • 1
    While the links may be helpful, [you should include the relevant information here](http://stackoverflow.com/help/how-to-answer) in case those links ever go down. – Mike Cluck Feb 29 '16 at 18:39