Reputation: 29720
I'm just reading this documentation about Javascript 1.2, but I'm wondering which version of Javascript is being used in the most popular browsers.
http://www.tutorialspoint.com/javascript/javascript_nested_functions.htm
Upvotes: 121
Views: 231949
Reputation: 14259
I found this to be very unsatisfactory, as it relies on the browser behaving in a specific way. A much more reliable way is to actually check if new additions of a specific version are actually present. I wrote this snippet that does exactly that, and returns the JavaScript version supported by the browser:
function getJSVersion() {
var version = undefined;
if (String.prototype.trim) {
version = 5;
if (Array.prototype.map) {
version = 6;
if (Array.prototype.includes) {
version = 7;
if (Object.values) {
version = 8;
if (Promise.prototype.finally) {
version = 9;
if (Array.prototype.flat) {
version = 10;
if (String.prototype.matchAll) {
version = 11;
if (String.prototype.replaceAll) {
version = 12;
if (Object.hasOwn) {
version = 13;
if (Array.prototype.toSorted) {
version = 14;
}
}
}
}
}
}
}
}
}
}
if (version) {
return "1." + version;
} else {
return "unknown";
}
}
You can e.g. output it into a browser like this:
window.onload = function () {
document.write("Supported JavaScript version: " + getJSVersion());
};
See snippet on jsFiddle.
Upvotes: 0
Reputation: 51
I decided to write a script based on the previous answers that gives you the language version inside the browser via binary search. Probably not useful but answers the question.
window.version=[0,0,0,0];
void async function main(){
let currentVersion = await loadVersion();
console.log(currentVersion);
}();
function tryVersion(num,i){
if(i==0){
return tryFirstVersion(num);
}
if(i==1){
return trySecondVersion(num);
}
if(i==2){
return tryThirdVersion(num);
}
if(i==3){
return tryFourthVersion(num);
}
}
function tryFirstVersion(num) {
return new Promise(function (resolve, reject) {
var s;
s = document.createElement('script');
s.innerHTML = 'window.version[0]='+num+';document.currentScript.onload();';
s.setAttribute('language','Javascript'+num+'.0');
s.onload = resolve;
document.head.appendChild(s);
var follow = document.createElement('script');
follow.innerHTML = 'document.currentScript.onload();';
follow.onload = resolve;
document.head.appendChild(follow);
});
}
function trySecondVersion(num) {
return new Promise(function (resolve, reject) {
var s;
s = document.createElement('script');
s.innerHTML = 'window.version[1]='+num+';document.currentScript.onload();';
s.setAttribute('language','Javascript'+version[0]+'.'+num);
s.onload = resolve;
document.head.appendChild(s);
var follow = document.createElement('script');
follow.innerHTML = 'document.currentScript.onload();';
follow.onload = resolve;
document.head.appendChild(follow);
});
}
function tryThirdVersion(num) {
return new Promise(function (resolve, reject) {
var s;
s = document.createElement('script');
s.innerHTML = 'window.version[2]='+num+';document.currentScript.onload();';
s.setAttribute('language','Javascript'+version[0]+'.'+version[1]+'.'+num);
s.onload = resolve;
document.head.appendChild(s);
var follow = document.createElement('script');
follow.innerHTML = 'document.currentScript.onload();';
follow.onload = resolve;
document.head.appendChild(follow);
});
}
function tryFourthVersion(num) {
return new Promise(function (resolve, reject) {
var s;
s = document.createElement('script');
s.innerHTML = 'window.version[3]='+num+';document.currentScript.onload();';
s.setAttribute('language','Javascript'+version[0]+'.'+version[1]+'.'+version[2]+'.'+num);
s.onload = resolve;
document.head.appendChild(s);
var follow = document.createElement('script');
follow.innerHTML = 'document.currentScript.onload();';
follow.onload = resolve;
document.head.appendChild(follow);
});
}
async function getVersion(i){
let upperLimit = 0;
let nextVersion = 0;
let doubling = true;
while(doubling){
nextVersion = version[i];
if(nextVersion==0){nextVersion=1;}else{nextVersion=nextVersion*2;}
await tryVersion(nextVersion,i);
if(nextVersion > version[i]){
upperLimit = nextVersion;
doubling = false;
}
}
let halving = true;
while(halving){
if(upperLimit<=(version[i]+1)){
halving=false;
}
nextVersion = Math.floor((version[i] + upperLimit)/2);
await tryVersion(nextVersion,i);
if(nextVersion > version[i]){
upperLimit = nextVersion;
}
}
return version[i];
}
async function loadVersion(){
await getVersion(0);
await getVersion(1);
await getVersion(2);
await getVersion(3);
return version;
}
Upvotes: 1
Reputation: 186984
Click on this link to see which version your BROWSER is using: http://jsfiddle.net/Ac6CT/
You should be able filter by using script tags to each JS version.
<script type="text/javascript">
var jsver = 1.0;
</script>
<script language="Javascript1.1">
jsver = 1.1;
</script>
<script language="Javascript1.2">
jsver = 1.2;
</script>
<script language="Javascript1.3">
jsver = 1.3;
</script>
<script language="Javascript1.4">
jsver = 1.4;
</script>
<script language="Javascript1.5">
jsver = 1.5;
</script>
<script language="Javascript1.6">
jsver = 1.6;
</script>
<script language="Javascript1.7">
jsver = 1.7;
</script>
<script language="Javascript1.8">
jsver = 1.8;
</script>
<script language="Javascript1.9">
jsver = 1.9;
</script>
<script type="text/javascript">
alert(jsver);
</script>
My Chrome reports 1.7
Blatantly stolen from: http://javascript.about.com/library/bljver.htm
Upvotes: 102
Reputation: 222461
In chrome you can find easily not only your JS version but also a flash version. All you need is to type chrome://version/
in a command line and you will get something like this:
Upvotes: 21
Reputation: 630349
Wikipedia (or rather, the community on Wikipedia) keeps a pretty good up-to-date list here.
Upvotes: 43
Reputation: 23034
Rather than finding which version you are using you can rephrase your question to "which version of ECMA script does my browser's JavaScript/JSscript engine conform to".
For IE :
alert(@_jscript_version); //IE
Refer Squeegy's answer for non-IE versions :)
Upvotes: 6
Reputation: 35021
JavaScript 1.2 was introduced with Netscape Navigator 4 in 1997. That version number only ever had significance for Netscape browsers. For example, Microsoft's implementation (as used in Internet Explorer) is called JScript, and has its own version numbering which bears no relation to Netscape's numbering.
Upvotes: 0
Reputation: 46735
All of todays browsers use at least version 1.5
:
http://en.wikipedia.org/wiki/ECMAScript#Dialect
Concerning your tutorial site, the information there seems to be extremely outdated, I beg you to head over to MDC and read their Guide:
https://developer.mozilla.org/en/JavaScript/Guide
You may still want to watch out for features which require version 1.6
or above, as this might give Internet Explorer some troubles.
Upvotes: 2