Search code examples
javascriptecmascript-5

Error as '`' (backquotes) not allowed in javascript (ecma5)


var now = new Date();

var year = now.getUTCFullYear();
var month = now.getUTCMonth() + 1;
var day = now.getUTCDate();
var hours = now.getUTCHours();
var minutes = now.getUTCMinutes();
var seconds = now.getUTCSeconds();
var milliseconds = now.getUTCMilliseconds();
var test = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
console.log(test);

Are backquotes not allowed in javascript (`) ? I am trying in ecma5 but getting error as mentioned above.

And also, can we match regular expression for above mentioned date-time format ?


Solution

  • Right now, the lastest version of JS is EcmaScript6, and many references online use EcmaScript13 specs.

    Backticks are not allowed in Ecmascript 5, they were added to serve easier access to multi-lined strings in EcmaScript6.

    You can use the + sign:

    let a = year+'-'+month+'-'+day+' '+hours+':'+minutes+':'+seconds;