JavaScript issue with Constants (numbers and strings) or is it just me

JavaScript issue with Constants (numbers and strings) or is it just me
0

#1

A thought came to mind to see if JavaScript’s const can be used with string. At first I ran some code on my own then went to this link from googling the question and went in here:

Although different language, as this is via JavaScript, I coded what I saw in the link above and ran it in JavaScript:

const HELLO3 = “What’s Up?”;
undefined

// Received an error.
console.log(HELLO3);
VM995:2 Uncaught ReferenceError: HELLO3 is not defined(…)(anonymous function) @ VM995:2InjectedScript._evaluateOn @ VM246:878InjectedScript._evaluateAndWrap @ VM246:811InjectedScript.evaluate @ VM246:667

// Versus within a regular variable
var Hello2 = “What’s up?”;
undefined

console.log(Hello2);
VM1338:2 What’s up?
undefined

???JAVASCRIPT issue (bug) with CONSTANT, am I wrong???

Example running this code in not only Chrome (Version 49.0.2623.112 (64-bit)) as I’m on OsX 10.8), but also within Safari. Then I tried it with what I originally know constants are used for, with numbers. I ran below sample example from:

// It would seem that there is issue with numbers as well.
const MY_FAV = 7;
undefined

// according to MDN, this next line of code is supposed to show an error, but 20 popped up as if correct output
MY_FAV = 20;
20

// ran the console to reveal that the later defined value (20) comes out (bug)
console.log(MY_FAV);
VM257:2 20
undefined

// decided to write this constant variable out again on the browser console console, just out of thought
const MY_FAV = 7;
undefined

// and the output came out as 20 when called for
console.log(MY_FAV);
VM435:2 20
undefined

// ran same const within Safari browser. (at first all code was executed in Chrome)

const MY_FAV = 7;
< undefined
console.log(MY_FAV);
[Log] 7
< undefined
MY_FAV = 20;
< 20
console.log(MY_FAV);
[Log] 20
< undefined
const MY_FAV = 7;
< undefined
console.log(MY_FAV);
[Log] 7
< undefined

// my Firefox browser caught the error though:

const MY_FAV = 7;
undefined
console.log(MY_FAV);
7
undefined

MY_FAV = 20;
TypeError: invalid assignment to const `MY_FAV’

Again,
???JAVASCRIPT issue (bug) with CONSTANT, or am I wrong???


#2

The const keyword was introduced as part of ES2015 (The 2015 version of JavaScript). Depending on the environment you are running in, it may or may not be available.

I suspect the HELLO3 example was run in an Environment that is using an older version of JavaScript. That’s why var worked, but const didn’t.

As for your other examples, I can not reproduce them in Chrome or Safari. For me, it works correctly.

Chrome:

const MY_VAR = 7
undefined
console.log(MY_VAR)
VM232:1 7
undefined
MY_VAR = 20
VM264:1 Uncaught TypeError: Assignment to constant variable.
    at <anonymous>:1:8

Safari:

> const MY_VAR = 7
< undefined
> console.log(MY_VAR)
[Log] 7
< undefined
> MY_VAR = 20
< TypeError: Attempted to assign to readonly property.

#3

You can check caniuse site to see if your browser can support const. Also, run console.log( navigator.userAgent ) to see which browser version you are currently running.


#4

Yes that is the reason as I found that out as a fact as I went further into research mode. Thank you @BillSourour.

Thanks for heads up @Reggie01, I found out that my Chrome browser is compatible. I’m finding out that my Safari is old and version 10 is compatible but I cannot download it for it is for version 10.10 and up.

So It looks like fireFox will be my primary with coding here and there for accuracy (another reason why it’s stated that one should run code in multiple browsers).

And my main thought question before this was with Constants and Strings and Firefox gave me the answer I was looking for; it can be used. Example:

Firefox version 48.0.2:

const MY_NAME = “Kingsley”;
undefined
console.log(MY_NAME);
Kingsley
undefined

While doing some further research, I found out I can use my old Safari browser with the use of “use strict”. Link below.

Safari Version 6.2.8 (with the use of “use strict”):

“use strict”
< "use strict"
const MY_VAR = 7;
< undefined
console.log(MY_VAR);
[Log] 7
< undefined
const MY_NAME = “Kingsley”;
< undefined
console.log(MY_NAME);
[Log] Kingsley

Chrome Version 49.0.2623.112 (64-bit):

Funny how normally when I’m browsing, I’m working through these browsers, above, from the bottom up; Chrome being primary, Safari secondary, and Firefox was on standby. Looks like I’ll be doing it vice version for coding with JavaScript, for now at least. I may be able to download Chrome version 50 though, I’ll have to get back to my notes on that link I found that does that.

I’m still thankful that my Macbook Pro (before the unibody version came out) still functions, just not up to day. I have other tools to use as a programmer that I loaded based on The Odin Project instructions, when I get to the backend of this journey.

NOTE: Two links that has helped me further in this question was:


with the use of “use strict”. Then I googled that to reach into MDN that talks further about “use strict”: