31

As the title says, I want to know the exact reason why JavaScript is called a "scripting language"?

My understanding is it's because JavaScript is only interpreted by the browser (and not compiled). Correct me if I'm wrong.

But if there is no compilation then how come 0 == '' is true? Doesn't the compiler coerce the values, or is that the JavaScript engine...? I am a bit confused.

7 Answers 7

44

I think first two sentences from wikipedia are clear enough:

A scripting language, script language or extension language is a programming language that allows some control of a single or many software application(s). Languages chosen for scripting purposes are often much higher-level than the language used by the host application...

In this case, the application is the browser. And about compilation:

Scripts are often, but not always, interpreted from the source code or "semi-compiled" to bytecode which is interpreted, unlike the applications they are associated with, which are traditionally compiled to native machine code for the system on which they run

About 0 being equal to '', the coercion it is not necessarily achieved by a compiler; it's all about the JavaScript engine in runtime.

I feel sorry for taking everything from Wikipedia but it's so clear and I put it quoted

PS: I find worth to paste this too:

Many people view 'scripting' languages as inferior or somehow different than languages that haven't achieved popularity on the scripting scene. Ironically, these same languages were carefully chosen for scripting due to their quality and versatility.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks victor for helping me..but any info abt my 2nd qs?
In Javascript == tries to covert the type of RHS to the left hand side if they are not equal. For this reason '' == 0 is false but 0 == '' is true. Same goes false == '0' being true. It is better to use === and !==
It's important to point out this answer as the more recent Node.js changes the context of this old answer
15

An update for 2017

"Scripting languages are a lot like obscenity. I can't define it, but I'll know it when I see it." - Larry Wall

For the purposes of this answer let's assume it to mean a language that:

  1. lacks some of the features of a "real" language (whatever that means) so is most useful as the "glue" between other components in the system, and
  2. is interpreted rather than compiled.

Javascript was indeed at one point considered a scripting language, with basic features to manipulate the DOM, perform form validation and make the Jesus dance. It was executed directly from source by an interpreter.

But JS has matured considerably over the last few years, with advanced features such as lambdas, classes (for better or worse), destructuring, iterators and modules that bring its capabilities on par with most other modern languages. No longer restricted to the browser, is it also commonly found running standalone on the server under NodeJS.

Javascript is now universally JIT compiled, either to bytecode (like Java and C#), or directly to machine code (like C and C++). And modern engines offer an optimization phase, similar to most traditional compiled languages.

V8 (Chrome, Node)

V8 compiles JavaScript directly to native machine code before executing it.

Chakra Code (Edge)

Chakra Core [can] do parallel JIT compilation...

SpiderMonkey (Firefox)

SpiderMonkey 38 includes a just-in-time compiler (JIT) that compiles JavaScript to machine code...

Therefore, if modern JS is considered a scripting language then the same should apply to most other non-"scripting" languages.

1 Comment

This is the best answer here because it actually explains why JavaScript started off as a scripting language (sent with html to run as a script when the page loaded) but has developed into a language with a versatile (and clearly powerful) use case.
10

You're partially right. A scripting language is basically a language that doesn't stand by itself; it "scripts" another application (in this case, the browser). I think what you're thinking of is an interpreted language. What that essentially means is that it isn't compiled (at least not in the traditional sense), it's "interpreted" from the source code. Your example actually has nothing to do with compilation. The type conversion from a string to an integer is done at runtime.

1 Comment

for clarity - a scripting language can be for the OS rather than an 'application' (enter argument about whether shells constitute applications) :)
7

To understand Why JavaScript is a Scripting Language? First, we understand Why Java is not a Scripting Language.

I think Scripting means a written text of a project, play etc. which is read by the players directly, no interpreter between player and Script.

Just same thinking in JavaScript our program acts like a play or movie which is written on a notepad or any other text pages like EditPlus, which is read by the browser directly through the JavaScript engine, no compiler and interpreter is required to display the output on the browser, browser act like a player.

But Java requires compiler and interpreter to convert the written Java coding in computer-readable format and display output. So Java is not a Scripting language and JavaScript is a Scripting language.

This description is my opinion about Why JavaScript is a Scripting language. I have tried to understand this problem, relate through real-life uses. I think you are satisfied.

1 Comment

I don't find this answer helpful, and I think it takes the word "script" too literally. I can see no distinction between a Javascript program "scripting" the browser, and a C program "scripting" the TTY to which is connected.
4

The idea of a scripting language is one that instructs a host to carry out a series of actions (a lot like an actor reading from a script).

Javascript tells a browser what to do and how to process things just the same way that a shell script, php, or any other scripting language does for their respective hosts.

1 Comment

"the same way that a shell script, php, or any other scripting language does for their respective hosts " - the same could be said about C# (the host here being the CLR).
3

it's an interpreted language that is sandboxed in its access and utilized for a particular purpose.

This means:

  • it is interpreted when run, it is not compiled
  • It is given limited access to the system, usually though a specific API
  • It is usually only given API calls that help it achieve its intended purpose and nothing more (though third-party additions can be used)

2 Comments

Actually it is compiled to bytecode on runtime. It just happens fast. Interpreted languages like Javascript, This is sometimes referred to as the Just-in-Time (JIT) compiler so basically it is "compiled"
"it is interpreted when run, it is not compiled" - this may have been true in the past, but modern JS engines perform compilation. "It is given limited access to the system, usually though a specific API" - the same could be said about Java. "... though third-party additions can be used" - the same could be said about .NET (eg. PInvoke).
0

JavaScript is called a scripting language because it was originally created to “script” the browser — that is, to automate and control behavior inside another host environment (like HTML pages in a web browser), not to build standalone applications like C or Java programs.

Historically, scripting languages were interpreted rather than compiled, which is where the confusion comes from. But in modern JavaScript engines (like V8, SpiderMonkey, etc.), the code is actually parsed and JIT-compiled (Just-In-Time) into machine code at runtime for performance. So today, JavaScript is both interpreted and compiled, depending on which part of the execution pipeline you look at.

As for 0 == '' being true — that happens due to type coercion rules defined in the ECMAScript specification, not because of compilation. When you use ==, JavaScript automatically converts both operands to a common type before comparing. Here, '' (an empty string) is converted to the number 0, so the comparison becomes 0 == 0, which is true.

If you want to avoid this kind of behavior, always use strict equality (===), which doesn’t perform type coercion.

2 Comments

There's a fairly high chance this is ai generated.
I understand. I usually just keep these kinds of notes to myself, but I thought this one might be helpful to post here since it adds a bit of explanation I didn’t see in other answers.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.