InterviewSolution
| 1. |
How to fill static values in an array in JavaScript? |
|
Answer» Let’s first understand what is an compiled language and interpreted language.
If no error it will generate an executable “hello” and we will run it by: ./hello
The above code does not need to be compiled first but it does require that python is installed on any machine that needs to run the script.
However there is a compilation step just before the interpretation step in JavaScript. So, JS is both compiled and interpreted language.
Let consider the below example. The compilation steps mainly looks at the var keyword. It is not bothered with what is assigned in these variables. var a = 10; var b = 20; console.log(a+b)When the compiler goes to line 1, it encounters var a and registers it in the global scope and then goes to line 3 and registers the var b. Line 5 is only a console and it doesn’t FINDS any var, so don’t do anything.
For the above example, the interpreter starts at line 1 and see a variable a and ask the compiler, if it have a variable “a” in Global scope and the compiler have it. So, it assigns the value 10 to it. NEXT the same step is repeated for line 3 and interpreter assigns 20 to variable “b”. Now once the interpreter goes to line 5, it finds console. It first looks for console at global scope from the compiler but don’t find it. So, it checks in the JavaScript global and finds it. INSIDE the console there are variable a and b, which it finds at global scope. It then adds them using addition operator and display the RESULT. |
|