Background
Here I got stuck once again behind that stolen code.
Scenario
The code was written in the JavaScript Language.
My target environment is JScript on an MS Windows Desktop.
I will be attempting to run it from the console using cscript.
Code
Original Code
Script
function getEpochDate() { const epoch = new Date( 1601, 0, 1 ); return epoch; }
Invoke
cscript getEpochDate.using.keyword.const.js
Output
Textual
>cscript getEpochDate.using.keyword.const.js Microsoft (R) Windows Script Host Version 5.812 Copyright (C) Microsoft Corporation. All rights reserved. T:\getEpochDate.using.keyword.const.js(4, 5) Microsoft JScript compilation error: Syntax error >
Revised Code
Script
/* Function getEpochDate as 1601-January-1st */ function getEpochDate() { // In Javascript Month is 0 based // That is January is indicated by 0 and not 1 var epoch = new Date( 1601, 0, 1 ); return epoch; } /* Declare Variables */ var dtMSWindowsEpoch; var strLog; /* Call function getEpochDate */ dtMSWindowsEpoch = getEpochDate(); /* Build out display buffer */ strLog = ""; strLog = strLog.concat("MS Windows Epoch date is ", dtMSWindowsEpoch.toString()); /* Emit display buffer */ WScript.StdOut.WriteLine(strLog);
Invoke
cscript getEpochDate.using.keyword.var.js
Output
Textual
>cscript getEpochDate.using.keyword.var.js Microsoft (R) Windows Script Host Version 5.812 Copyright (C) Microsoft Corporation. All rights reserved. MS Windows Epoch date is Mon Jan 1 00:00:00 PST 1601 >
Summary
Keywords
Const
See the const was only added in 2015.
ECMAScript version 6 (or ES6, ES2015, ES6Harmony), formerly known as “ECMA-262 ECMAScript Language Specification 2015”.
The var, let and const Variable Declarations
Prior to ES6, you could only use the keyword var to declare a variable.
Microsoft’s Jscript implementation was way before 2015.
JScript was first released in 1996.
It’s last stable is Version 9.
The v9.0 version was released in March 2011.
So please stay with defining variables using the var keyword.
Obviously this restraint only applies us to us old head targeting MS Windows.