[javascript] Is JavaScript a compilation language or an interpreter language?

Asked 2 years ago, Updated 2 years ago, 80 views

Hello. While studying scope in JavaScript, I was reading the following link, and I have a question.

https://meetup.toast.com/posts/86

function foo() {
    console.log(a);
    var a = 2;
}
foo();

There is an article in the text as below.

This time, undefined is printed. It may feel a little absurd, but it's not that absurd when it turns out. The JavaScript engine first compiles the code before interpreting it. Var = 2; may be considered as one syntax, but JavaScript is divided into the following two syntaxes.

Usually, C language is a compilation language and Java is an interpreter language. Looking at the text above, JavaScript seems to be interpreting and compiling. Why does JavaScript have both functions? And is JavaScript an interpreter language? Is it a compilation language?

Thank you for reading the question :)

javascript hoisting compiler interpreter

2022-09-22 18:44

2 Answers

Hello!

Before talking about interpreter language or compilation language, I would like you to look at the contents below. In the cited part

The JavaScript engine first compiles the code before interpreting.

That's what it starts with. An interpretation engine is required to execute JavaScript code. JavaScript has several engines, one implemented only as a compiler, and one interpreter.

In the past, you could say, 'This is an interpreter language, that is a compilation language,' but in fact, the boundaries have become somewhat blurred since the advent of the JIT compiler and VM.

var a = 2;

The JavaScript engine first compiles the code before interpreting it. Var = 2; may be considered as one syntax, but JavaScript is divided into the following two syntaxes.

var a;

a = 2;

Answer 1. My interpretation of this article seems that 'compile' in one compiler before parsing process and ' before 'interpreting' is analysis and code generation.

Answer2. There is no official statement that JavaScript is an interpreter language. But if you have to tell the difference,

Have a nice day


2022-09-22 18:44

Please note that java is also a compilation language.

The explanation of the text is quite correct.
You can understand the meaning of each term.
Interpreting: Interpreting and executing code as it is read.
Compilation: To convert code written in one language into another.

This is why java executes the code by compiling in another language called byte code, and then jvm executes the byte code by interpreting.
Js goes through the same process.
Rather than reading and executing the js code right away, compile in a more machine-friendly language, and then interprint the result.
However, since js is usually called interpreter language, it may be annoying if you say it's a compilation language for no reason.

Wiki is also explained.

The JavaScript engine may be a traditional interpreter, or it may be able to compile JIT in byte code in certain ways.

The reason why both compiling and interpreting processes are involved is to gain both advantages.
Compilation takes a long time, but the result is fast and interpreter language has no compilation process, so it saves time and makes frequent code modifications easy.


2022-09-22 18:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.