46

How can I detect what version of JavaScript the browser supports (if any)? I want to check for ECMAScript 3 versus ECMAScript 5 versus ECMAScript 6.

Note: I want to avoid the deprecated language tag.

Randomblue
  • 112,777
  • 145
  • 353
  • 547
  • 3
    Do you really need to check version or checking features will be enough? (`if (typeof new Array().forEach === "function") alert("we have forEach")`) – Ivan Nevostruev Sep 07 '11 at 21:31
  • 3
    I would strongly recommend feature detection over version detection. It's a lot easier to maintain over time and usually it more accurately tests what you really care about. Remember, a given ECMAScript 5 implementation isn't all or nothing. Many browsers will have some elements of a new version, but not all. – jfriend00 Sep 07 '11 at 21:34
  • You may also find this useful http://kangax.github.io/compat-table/es5/ – Brian Duncan Jun 02 '15 at 15:16
  • Related question about ES6 modules, with " 'noModule' in HTMLScriptElement.prototype " as a feature detection solution: https://stackoverflow.com/questions/27922232/how-to-feature-detect-es6-modules – Stefan Oct 23 '19 at 07:59
  • Does this answer your question? [How do I know which version of Javascript I'm using?](https://stackoverflow.com/questions/4271566/how-do-i-know-which-version-of-javascript-im-using) – Ravi Parekh Sep 23 '22 at 13:46
  • Duplicate - https://stackoverflow.com/questions/4271566/how-do-i-know-which-version-of-javascript-im-using – Ravi Parekh Sep 23 '22 at 13:46

5 Answers5

22

Here is a good reference for you: http://www.docsteve.com/DocSteve/Samples/JS/js_version.html

Basically, use the deprecated language attribute of the script tag

console.log("javascript version =", js_version)
<script language="javascript">var js_version="1.0"</script>
<script language="javascript1.1">var js_version="1.1"</script>
<script language="javascript1.2">var js_version="1.2"</script>
<script language="javascript1.3">var js_version="1.3"</script>
<script language="javascript1.4">var js_version="1.4"</script>
<script language="javascript1.5">var js_version="1.5"</script>
<script language="javascript1.6">var js_version="1.6"</script>
Naftali
  • 144,921
  • 39
  • 244
  • 303
  • This is what I was going to suggest, even though it uses the deprecated attribute `language`. You still need to map JS version to ECMAScript versions. http://en.wikipedia.org/wiki/ECMAScript#Version_correspondence – Ruan Mendes Sep 07 '11 at 21:31
  • @Juan -- ehh true, but you cannot really prevent that in this case. – Naftali Sep 07 '11 at 21:33
20

The solution proposed to http://www.docsteve.com/DocSteve/Samples/JS/js_version.html as simple JavaScript function (using "createElement" construction):

<script type="text/javascript">
  function get_js_version ()
  {
    this.jsv = {
        versions: [
          "1.1", "1.2", "1.3", "1.4", "1.5", "1.6", "1.7", "1.8", "1.9", "2.0"
        ],
        version: ""
      };

    var d = document;

    for (i = 0; i < jsv.versions.length; i++) {
      var g = d.createElement('script'),
        s = d.getElementsByTagName('script')[0];

        g.setAttribute("language", "JavaScript" + jsv.versions[i]);
        g.text = "this.jsv.version='" + jsv.versions[i] + "';";
        s.parentNode.insertBefore(g, s);
    }

    return jsv.version;
  }

  document.write('JavaScript Version: ' + get_js_version());
</script>
DmitryS
  • 261
  • 2
  • 3
  • nice solution to temporarily inject and erase after detecting. no library integration required. – ahnbizcad Nov 16 '15 at 21:17
  • I couldnt find anywhere info about JS version 1.9 and 2.0, looks like latest is 1.8.5, or ? https://en.wikipedia.org/wiki/JavaScript#Version_history – Josef Vancura Aug 11 '17 at 09:18
12

I suppose it depends on what you want to do with the information, but many people prefer to do feature detection, instead of figuring out what browser someone is using or what version of JS.

Check out Modernizr, which is a great library that does feature detection for you.

Matt
  • 41,216
  • 30
  • 109
  • 147
  • Okay, then what does it mean that fromCodePoint() and fromCharCode() functions are undefined? According to the get_js_version function mentioned below, I'm running 1.5. I thought these functions were in 1.5... – David Spector Oct 24 '18 at 16:07
  • These functions seem to be missing from Firefox 63.0. I have submitted a bug report. – David Spector Oct 24 '18 at 16:39
10

This pops out an alert box with the javascript version being used by your browser:

<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>

Related jsfiddle.net

Chris Forrence
  • 10,042
  • 11
  • 48
  • 64
Vickar
  • 923
  • 11
  • 16
  • Whilst this may theoretically answer the question, [it would be preferable](//meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – James Fenwick Jun 13 '16 at 10:39
  • Thanks James to bring that meta question to my notice, I'll mind that in future. – Vickar Jun 14 '16 at 06:07
  • 1
    Thanks, very useful as we don't need to copy anything! – Nicofisi Mar 14 '17 at 09:46
0

Here is an extended ECMAScript detector (based off @Infigon's answer).

const ecmaScriptInfo = (function() {
                     // () => { is not allowed
  function getESEdition() {
    const array = [];
    switch (true) {
      case !Array.isArray:
        return 3;
      case !window.Promise:
        return 5;
      case !array.includes:
        return 6;
      case !''.padStart:
        return 7;
      case !Promise.prototype.finally:
        return 8;
      case !window.BigInt:
        return 9;
      case !Promise.allSettled:
        return 10;
      case !''.replaceAll:
        return 11;
      case !array.at:
        return 12;
      default:
        return 13;
    }
  }

  function getESYear(edition) {
    return {
      3: 1999,
      5: 2009
    }[edition] || 2009 + edition;
  }
  
  const edition = getESEdition();
  const year = getESYear(edition);

  return {
    edition: edition, // usually shortened [edition,]
    year: year,       // usually shortened [year,]
    text: 'Edition: '+ edition +' | Year: '+ year
       // `Edition: ${edition} | Year: ${year}` is not allowed
  }
})();

console.log(ecmaScriptInfo.edition);
console.log(ecmaScriptInfo.year);
console.log(ecmaScriptInfo.text);

The additions and changes are:

  • It adds a function closure to create private functions and variables.
  • It uses a switch for better performance.
  • It fixes the incorrect year for edition 5, by the use of an object literal, which was released 2009 (not 5 + 2009). A nullish coalescing would have been used, instead of an OR (||), but it did not exist in earlier editions.
  • It returns the values as an object that can be used however one wishes to. For example, console.log(ecmaScriptInfo.edition).

Note:

The Arrow Function Expression was introduced in edition 10 (2019); hence why a standard function call was used.

Object literal shared key and value shorthand came in later editions; hence why edition: edition was not shortened.

Template Literals was introduced in edition 9 (2018); hence why Edition: ${edition} | Year: ${year} was not used.

ckhatton
  • 1,359
  • 1
  • 14
  • 43