What's the syntax difference of JavaScript between node.js and browsers? - javascript

Browsers support JavaScript, and Node.js supports it too. I want to know if there any syntax difference between them?

Node uses Google V8, which implements the ECMAScript standard (link to non official annotated copy).
How it differs from browsers will depend on which browser (and version) you're talking about.
For example, Mozilla browsers implement JavaScript (which is an implementation and superset of ECMAScript).
JavaScript includes:
for each - in loops
destructuring assignment
let expressions
array comprehensions
...among other enhancements that utilize non ECMAScript standard syntax. These are all part of JavaScript, but not currently part of the ECMAScript standard.
(Of the 4 items listed, the last 3 are proposals for the next ECMAScript version.)

No. The Syntax is exactly the same. There are differences in the apis however. The standard browser dom is not available in node but it has additional apis found at nodejs.org. Any syntax differences are due to quirks in browsers.

No. The syntax is exatcly the same, but you're working it provides with different environment - for example, you don't have DOM and have API for file system access and sockets.

Related

Can I run a language based on the ECMAScript specification like JScript or ActionScript in current browsers?

I have a very big doubt and it is that if ActionScript and JScript are based on ECMAScript, it is possible that they can be executed in environments like Google Chrome and if not, why not?
Any code that 100% conforms to supported versions of ECMAScript can be run in Chrome. But, you can't actually do anything useful in a browser with pure ECMAScript code because you can't interact with anything in the browser itself - instead you have to use browser-specific APIs to do that which are not ECMAScript methods, but browser-specific methods that are documented in different standards. Similarly, an ActionScript script likely interacts with its environment through non-ECMAScript methods which are not support in a browser.
And a new language that conforms to 100% to the latest version of ECMAScript would not have problems with the interpreter of each browser? I say this because every browser has a dedicated JavaScript interpreter or that's what I understand so far.
It's hard to tell what you're asking. There is no such thing as a new language that conforms to 100% of ECMAScript. That would just be an implementation of ECMAScript (not a new and different language) - just like each browser has its own implementation of ECMAScript. If it is indeed a new language, then it must have things in it that are not in ECMAScript. But, my point is that, pure ECMAScript code should run in any "compatible" ECMAScript implementation that is targeting the same ECMAScript version.
There are details of implementation that are not always complete in each browser since ECMAScript is a moving target (new revisions regularly) and the development on the implementation is ongoing work in each version. So, there can be compatibility issues around the edges of newish features, but that is true of any Javascript code running across browsers that is leverage very new features. Developers who are interested in good cross browser compatibility either stay away from the very latest features, cross compile their code to a lower common denominator using something like Babel or TypeScript or they study and test exactly what they can and can't use across all their target browsers.

Exactly which JavaScript version does Node.js understand by default?

If I use Node.js without a transpiler, i.e. something as simple as
node hello.js
what language is hello.js? I thought that, since Node.js is based on V8, it would be V8's language (presumably something close to a recent version of ECMAScript).
But Node also supports the require() function, which isn't in ECMAScript (or in V8).
Perhaps Node's language is V8 plus a subset of CommonJS? What subset?
As with every JavaScript engine out there, Node.js supports:
A collection of features from various versions of JS (since they get implemented feature-by-feature (as opposed to a single big release where ever change in a new version of ECMAScript is released at once). https://node.green/ maintains a list of features in various versions of ECMAScript and the support for them in different engines.
A bunch of extensions in the form of APIs that aren't defined by the ECMAScript specification (e.g. for filesystem access and CommonJS module support)

Is Node.js a JavaScript runtime environment or an EcmaScript environment?

According to https://nodejs.org/en/
Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine.
However, according to this answer, JavaScript is:
JavaScript = ECMAScript + DOM API;
and there is no DOM in Node.js runtime. (or is there?) Also quoting from the book: Professional JavaScript for Web Developers:
Though JavaScript and ECMAScript are often used synonymously,
JavaScript is much more than just what is defined in ECMA-262. Indeed,
a complete JavaScript implementation is made up of the following three
distinct parts:
The Core (ECMAScript)
The Document Object Model (DOM)
The Browser Object Model (BOM)
which supports the answer I link to.
Considering the facts above, isn 't it correct to say Node.js is a ECMAScript runtime? If not, what do we need to remove (or add?) if we wanted to fork Node.js and make it an ECMAScript runtime instead of a JavaScript runtime?
The difference between JavaScript and ECMAScript is just a pedantic difference that most people don't really care about. Technically the official language name according to the standard is ECMAScript, but for historical and convenience reasons people almost always call it "JavaScript". I've never met anybody who calls themselves an "ECMAScript developer".
As far as I know, JavaScript doesn't even have an official formal definition. While ECMAScript has a prescriptive definition with versions that are formally defined by their various specifications, the meaning of "JavaScript" is essentially defined by how people use it.
For this reason, Node.js advertises itself as a JavaScript runtime. It might be more "technically correct" to call it an ECMAScript runtime, but it would be more confusing for most people to advertise it that way.
There are many JavaScript run time environment:
Browsers
Node.js
Deno
Edge Workers
Moddable
Databses
Productivity Software
source: https://www.youtube.com/watch?v=JN7UjvceOlw

When did javascript add "touch" events

I would be interested to know a little about the history of the process of implementing touch events into the javascript language, such as when, but, better yet, the process by which the javascript developers decided on the rules for the touch api (is it an api?).
Also, do all touch devices adhere to a standard set of rules ... they would have to, right? otherwise how could javascript's touch api be cross-device dependable.
EDIT: wouldn't implementing a touch api into javascript constitute a new version of javascript?
wouldn't implementing a touch api into javascript constitute a new version of javascript?
Its important to recognize that the word Javascript can refer to many different things. There's the official ECMAScript spec, and then there are various implementations by different browsers and server-side environments. These implementations all implement different parts of the ECMAScript spec. Generally they each include some subset of the spec, along with some proprietary features as well. There were implementations of touch APIs (going back to safari on the original iPhone at least) well before it was part of the official spec.
This seems to be the spec that defines these events http://www.w3.org/TR/touch-events/, where it says:
Implementations that use ECMAScript to implement the APIs defined in
this specification must implement them in a manner consistent with the
ECMAScript Bindings defined in the Web IDL specification [WEBIDL] as
this specification uses that specification and terminology.
JavaScript is ECMAScript, or if you will, the ECMAScript implementation from Mozilla.

What is the difference between JavaScript and ECMAScript?

What's the difference between ECMAScript and JavaScript? From what I've deduced, ECMAScript is the standard and JavaScript is the implementation. Is this correct?
I think a little history lesson is due.
JavaScript was originally named Mocha and changed to Livescript but ultimately became JavaScript.
It's important to note that JavaScript came before ECMAscript and the history will tell you why.
To start from the beginning, JavaScript derived its name from Java and initially Brendan Eich (the creator of JS) was asked to develop a language that resembled Java for the web for Netscape.
Eich, however decided that Java was too complicated with all its rules and so set out to create a simpler language that even a beginner could code in. This is evident in such things like the relaxing of the need to have a semicolon.
After the language was complete, the marketing team of Netscape requested Sun to allow them to name it JavaScript as a marketing stunt and hence why most people who have never used JavaScript think it's related to Java.
About a year or two after JavaScript's release in the browser, Microsoft's IE took the language and started making its own implementations such as JScript. At the same time, IE was dominating the market and not long after Netscape had to shut its project.
Before Netscape went down, they decided to start a standard that would guide the path of JavaScript, named ECMAScript.
ECMAScript had a few releases and in 1999 they released their last version (ECMAScript 3) before they went into hibernation for the next 10 years. During this 10 years, Microsoft dominated the scenes but at the same time they weren't improving their product and hence Firefox was born (led by Eich) and a whole heap of other browsers such as Chrome, Opera.
ECMAScript released its 5th Edition in 2009 (the 4th edition was abandoned) with features such as strict mode. Since then, ECMAScript has gained a lot of momentum and is scheduled to release its 6th Edition in a few months from now with the biggest changes its had thus far.
You can use a list of features for ECMAScript 6 here http://kangax.github.io/es5-compat-table/es6/ and also the browser support. You can even start writing Ecmascript 6 like you do with CoffeeScript and use a compiler to compile down to Ecmascript 5.
Whether ECMAScript is the language and JavaScript is a dialect is arguable, but not important. If you continue to think like this it might confuse you. There is no compiler out there that would run ECMAScript, and I believe JavaScript is considered the Language which implements a standard called ECMAScript.
There are also other noticeable languages that implement ECMAScript such as ActionScript (used for Flash)
ECMAScript is the language, whereas JavaScript, JScript, and even ActionScript 3 are called "dialects". Wikipedia sheds some light on this.
ECMAScript = ES:
ECMAScript is a Standard for scripting languages.
Languages like Javascript are based on the ECMAScript standard.
ECMA Standard is based on several originating technologies, the most well known being JavaScript (Netscape) and JScript (Microsoft).
ECMA means European Computer Manufacturer’s Association
JavaScript = JS:
JavaScript is the most popular implementation of the ECMAScript Standard.
The core features of Javascript are based on the ECMAScript standard, 
but Javascript also has other additional features that are not in
the ECMA specifications/standard.
ActionScript and JScript are other languages that implement the ECMAScript.
JavaScript was submitted to ECMA for standardization but due to trademark issues with the name Javascript the standard became called ECMAScript.
Every browser has a JavaScript interpreter.
ES5 = ECMAScript 5:
ES5 is a version of the ECMAScript (old/current one).
ES5 is the JavaScript you know and use in the browser today.
ES5 does not require a build step (transpilers) to transform it into something that will run in today's browsers.
ECMAScript version 5 was finished in December 2009, 
the latest versions of all major browsers (Chrome, Safari, Firefox, and IE) 
have implemented version 5.
Version 5.1 was finished in June, 2011.
ES6 = ECMAScript 6 = ES2015 = ECMAScript 2015:
ES2015 is a version of the ECMAScript (new/future one).
Officially the name ES2015 should be used instead of ES6.
ES6 will tackle many of the core language shortcomings addressed in 
TypeScript and CoffeeScript.
ES6 is the next iteration of JavaScript, but it does not run in today's browsers.
There are quite a few transpilers that will export ES5 for running in browsers.
BabelJS:
BabelJS is the most popular transpiler that transforms new JavaScript ES6 to Old JavaScript ES5.
BabelJS makes it possible for writing the next generation of JavaScript today (means ES2015).
BabelJS simply takes ES2015 file and transform it into ES5 file.
Current browsers versions can now understand the new JavaScript code (ES2015), even if they don't yet support it.
TypeScript and CoffeeScript:
Both provides syntactic sugar on top of ES5 
and then are transcompiled into ES5 compliant JavaScript. 
You write TypeScript or CoffeeScript then the transpiler transforms it into ES5 JavaScript.
The umbrella term:
JavaScript = ECMAScript + DOM + BOM;
ECMAScript® Language Specification defines all logic for creating and editing objects, arrays, numbers, etc...
DOM (Document Object Model) makes it possible to communicate with HTML/XML documents (e.g. document.getElementById('id');).
BOM (Browser Object Model) is the hierarchy of browser objects (e.g. location object, history object, form elements).
History of JavaScript naming:
Mocha ► LiveScript ► JavaScript (the language) ► (part of JS resulted in) ECMA-262 ► ECMAScript ► JavaScript (umbrella term, consisting of ECMAScript + DOM + BOM)
Technically ECMAScript is the language that everyone is using and implementing -- it is the specification created many years ago when Netscape and Microsoft sat down and attempted to standardise the scripting between JavaScript (Netscape's scripting language) and JScript (Microsoft's).
Subsequently all these engines are ostensibly implementing ECMAScript, however JavaScript (the name) now hangs around for both traditional naming reasons, and as a marketing term by Mozilla for their various non-standard extensions (which they want to be able to actually "version")
Various JavaScript versions are implementations of the ECMAScript standard.
In my understanding, ECMAScript is the "Theory" or "Specification", and Javascript is "Practicals" or "Implementation".
i know this is an old post but hopefully this will help someone.
In the 1990’s different versions of js started coming out like javascript from netscape,
Js script from Microsoft. So ecmascript was introduced as a standard. But ecmascript forms only a part of javascript which specifies its core syntax,types,objects etc. Probably that explains the inconsistent implementations of javascript across diff. browsers
Reference - Wrox(Professional Javascript For Web Developers)
JavaScript is a ECMAScript language.
ECMAScript isn't necessarily JavaScript.
Here are my findings:
JavaScript: The Definitive Guide, written by David Flanagan provides a very concise explanation:
JavaScript was created at Netscape in the early days of the Web, and
technically, "JavaScript" is a trademark licensed from Sun
Microsystems (now Oracle) used to describe Netscape's (now Mozilla's)
implementation of the language. Netscape submitted the language for
standardization to ECMA and because of trademark issues, the
standardized version of the language was stuck with the awkward name
"ECMAScript". For the same trademark reasons, Microsoft's version of
the language is formally known as "JScript". In practice, just about
everyone calls the language JavaScript.
A blog post by Microsoft seems to agree with what Flanagan explains by saying..
ECMAScript is the official name for the JavaScript language we all
know and love.
.. which makes me think all occurrences of JavaScript in this reference post (by Microsoft again) must be replaced by ECMASCript. They actually seem to be careful with using ECMAScript only in this, more recent and more technical documentation page.
w3schools.com seems to agree with the definitions above:
JavaScript was invented by Brendan Eich in 1995, and became an ECMA
standard in 1997. ECMA-262 is the official name of the standard.
ECMAScript is the official name of the language.
The key here is: the official name of the language.
If you check Mozilla 's JavaScript version pages, you will encounter the following statement:
Deprecated. The explicit versioning and opt-in of language
features was Mozilla-specific and are in process of being removed.
Firefox 4 was the last version which referred to a JavaScript version
(1.8.5). With new ECMA standards, JavaScript language features are now
often mentioned with their initial definition in ECMA-262 Editions
such as ECMAScript 2015.
and when you see the recent release notes, you will always see reference to ECMAScript standards, such as:
The ES2015 Symbol.toStringTag property has been implemented (bug 1114580).
The ES2015 TypedArray.prototype.toString() and
TypedArray.prototype.toLocaleString() methods have been implemented
(bug 1121938).
Mozilla Web Docs also has a page that explains the difference between ECMAScript and JavaScript:
However, the umbrella term "JavaScript" as understood in a web browser
context contains several very different elements. One of them is the
core language (ECMAScript), another is the collection of the Web APIs,
including the DOM (Document Object Model).
Conclusion
To my understanding, people use the word JavaScript somewhat liberally to refer to the core ECMAScript specification.
I would say, all the modern JavaScript implementations (or JavaScript Engines) are in fact ECMAScript implementations. Check the definition of the V8 Engine from Google, for example:
V8 is Google’s open source high-performance JavaScript engine, written
in C++ and used in Google Chrome, the open source browser from Google,
and in Node.js, among others. It implements ECMAScript as specified in
ECMA-262.
They seem to use the word JavaScript and ECMAScript interchangeably, and I would say it is actually an ECMAScript engine?
So most JavaScript Engines are actually implementing the ECMAScript standard, but instead of calling them ECMAScript engines, they call themselves JavaScript Engines. This answer also supports the way I see the situation.
Existing answers paraphrase the main point quite well.
The main point is that ECMAScript is the bare abstract language, without any domain specific extensions, it's useless in itself. The specification defines only the language and the core objects of it.
While JavaScript and ActionScript and other dialects add the domain specific library to it, so you can use it for something meaningful.
There are many ECMAScript engines, some of them are open source, others are proprietary. You can link them into your program then add your native functions to the global object so your program becomes scriptable. Although most often they are used in browsers.
ECMAScript is a standard for JavaScript, look at these statements from MDN:
JavaScript
JavaScript (JS) is a lightweight interpreted or JIT-compiled
programming language with first-class functions. While it is most
well-known as the scripting language for Web pages, many non-browser
environments also use it, such as node.js, Apache CouchDB and Adobe
Acrobat. JavaScript is a prototype-based, multi-paradigm, dynamic
language, supporting object-oriented, imperative, and declarative
(e.g. functional programming) styles. Read more about JavaScript.
ECMAScript
The standard for JavaScript is ECMAScript. As of 2012, all modern
browsers fully support ECMAScript 5.1. Older browsers support at least
ECMAScript 3. On June 17, 2015, ECMA International published the sixth
major version of ECMAScript, which is officially called ECMAScript
2015, and was initially referred to as ECMAScript 6 or ES6. Since
then, ECMAScript standards are on yearly release cycles. This
documentation refers to the latest draft version, which is currently
ECMAScript 2018.
For more info, visit here
Javascript was the original name, meant to capitalize on the popularity of java. ECMA is the standards body that oversees the standard that was eventually put in place so the names are roughly equivalent.
Implementations have other names, like V8 or Rhino, etc.
I doubt we'd ever use the word "ECMAScript" if not for the fact that the name "JavaScript" is owned by Sun. For all intents and purposes, the language is JavaScript. You don't go to the bookstore looking for ECMAScript books, do you?
It's a bit too simple to say that "JavaScript" is the implementation. JScript is Microsoft's implementation.
JavaScript is one branch of languages formed around the ECMAScript standard. I believe ECMA is the European Computer Manufacturers Association, not that this is really relevant or anything.
Don't forget another popular language formed around the ECMA Script standard is ActionScript, used in Adobe Flash/Flex.

Categories

Resources