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

**URL:** <https://forum.freecodecamp.org/t/javascript-issue-with-constants-numbers-and-strings-or-is-it-just-me/89170>\
**Category:** General\
**Created:** [February 19, 2017, 11:32pm UTC](https://forum.freecodecamp.org/t/javascript-issue-with-constants-numbers-and-strings-or-is-it-just-me/89170 "2017-02-19T23:32:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kingzlo](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kingzlo/32/39948_2.png) [@kingzlo](https://forum.freecodecamp.org/u/kingzlo)\
**Post date:** [February 19, 2017, 11:32pm UTC](https://forum.freecodecamp.org/t/javascript-issue-with-constants-numbers-and-strings-or-is-it-just-me/89170/1 "2017-02-19T23:32:39Z")

</div>

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:

> <https://stackoverflow.com/questions/1431576/how-do-you-declare-string-constants-in-c>

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:

> **[const - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const)**
>
> The const declaration declares block-scoped local variables. The value of a constant can't be changed through reassignment using the assignment operator, but if a constant is an object, its properties can be added, updated, or removed.

> // 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???_**

---

<div class="post-metadata">

**Author:** ![BillSourour](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/billsourour/32/40437_2.png) [@BillSourour](https://forum.freecodecamp.org/u/BillSourour)\
**Post date:** [February 19, 2017, 11:59pm UTC](https://forum.freecodecamp.org/t/javascript-issue-with-constants-numbers-and-strings-or-is-it-just-me/89170/2 "2017-02-19T23:59:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Reggie01](https://avatars.discourse-cdn.com/v4/letter/r/8c91f0/32.png) [@Reggie01](https://forum.freecodecamp.org/u/Reggie01)\
**Post date:** [February 20, 2017, 12:58am UTC](https://forum.freecodecamp.org/t/javascript-issue-with-constants-numbers-and-strings-or-is-it-just-me/89170/3 "2017-02-20T00:58:15Z")

</div>

You can check [caniuse](http://caniuse.com/#feat=const) site to see if your browser can support const. Also, run console.log( navigator.userAgent ) to see which browser version you are currently running.

> **[NavigatorID.userAgent](https://developer.mozilla.org/en-US/docs/Web/API/NavigatorID/userAgent)**
>
> The NavigatorID.userAgent read-only property returns the user agent string for the current browser.

---

<div class="post-metadata">

**Author:** ![kingzlo](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kingzlo/32/39948_2.png) [@kingzlo](https://forum.freecodecamp.org/u/kingzlo)\
**Post date:** [February 20, 2017, 3:43am UTC](https://forum.freecodecamp.org/t/javascript-issue-with-constants-numbers-and-strings-or-is-it-just-me/89170/4 "2017-02-20T03:43:09Z")

</div>

> [@BillSourour](#):
>
> 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.

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

> [@Reggie01](#):
>
> You can check caniuse site

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:

> <https://stackoverflow.com/questions/24008366/using-ecmascript-6>

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

> **[Strict mode - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode)**
>
> JavaScript's strict mode is a way to opt in to a restricted variant of JavaScript, thereby implicitly opting-out of "sloppy mode". Strict mode isn't just a subset: it intentionally has different semantics from normal code. Browsers not supporting...

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 16, 2021, 5:24pm UTC](https://forum.freecodecamp.org/t/javascript-issue-with-constants-numbers-and-strings-or-is-it-just-me/89170/5 "2021-01-16T17:24:00Z")

</div>


