0

I'm going to have to apologize for how simple this question is going to be. I am a literally on Chapter 2 trying to learn javascript, and only just preparing myself for what is to come.

My questions specifically refers to Chapter 2 of Eloquent Javascript's section on Variables. In the websites JS sandbox it uses the example:

var luigisDebt = 140;
luigisDebt = luigisDebt - 35;
console.log(luigisDebt);
// → 105
105

Got it no problem! Test around a bit more:

var luigisDebt = 140;
luigisDebt = luigisDebt - 35;
luigisDebt = luigisDebt - 5
console.log(luigisDebt);
100

However if I mess around with the capitalization like this:

var luigisDebt = 140;
luigisDebt = luigisDebt - 35;
luigisDebt = luigisdebt - 5
console.log(luigisDebt);
95

My output becomes incorrect with 95, instead of the correct 100.

Thanks to other posts I've learned about camelCase, constructer functions and PascalCase. But still can't figure out why my output changes with Upper and Lower case?

Vili
  • 13
  • 4
  • As `luigisdebt` is undefined in that context, the result should be 105. `luigisdebt` has probably been defined before and set to 100. – cdm Jul 28 '17 at 07:14
  • Did you assign value to `luigisdebt` before, you code should output `Uncaught ReferenceError: luigisdebt is not defined` – gy134340 Jul 28 '17 at 07:16
  • check how you define the lower case variable "luigisdebt" – caoglish Jul 28 '17 at 07:21
  • @gy134340 I did not assign any value to "luigidebt". I was expecting an error as well, that's why I found this to be so weird. Oh just a side note the code environment in the link I provided is interactive. – Vili Jul 28 '17 at 07:25
  • @caoglish Do you think the "luigidebt" was defined in the sandbox's backstage? – Vili Jul 28 '17 at 07:27
  • @cdm is there anyway to check? – Vili Jul 28 '17 at 07:27
  • @vili how about you change the variable name, like: luigisDebt => luigisDebt11 ,luigisdebt => luigisdebt11. see what will happen. – caoglish Jul 28 '17 at 07:30
  • @caoglish Yup that gave me what I was expecting. "ReferenceError: luigisdebt11 is not defined (line 3)" Does this mean that the "luigisdebt" was predefined? – Vili Jul 28 '17 at 07:33
  • @vili, it have to be defined in somewhere. console.log(luigisdebt) before ```var luigisDebt = 140;``` to see what's the value. and do the global search. This is why we do not use global variable because you do not know where global variable have been initialized. – caoglish Jul 28 '17 at 07:38
  • That does make sense @caoglish! So could I assume that if I wrote this line of code in an external editor I should expect an immediate "ReferenceError:" instead of an incorrect output? – Vili Jul 28 '17 at 07:48
  • @vili Yes, it's what I mean.However, does not mean that put the code in external editor. I mean that you should put the code in different runtime enviroment. For example, copy the code in your developer mode console of any browser. – caoglish Jul 29 '17 at 04:38
  • @caoglish I did just that and I receive an error. So I think I have my answer. Thank you very much! – Vili Jul 29 '17 at 07:57

1 Answers1

1

luigisdebt is not defined in your code, it should answer 105. define this before using it.

Prasanna
  • 1,752
  • 1
  • 15
  • 27
  • I understand that it is not defined in the code. Im looking more to understand how a lower cased "luigisdebt" subtracted 10 instead of 5. Also if this is caused by the sandbox environment of the book's website or if this is a kind of error I should keep an eye out for? – Vili Jul 28 '17 at 07:43
  • I believe that you are getting an error from some sandbox environment, kindly install node in your system and try.it won't throw error like this – Prasanna Jul 28 '17 at 12:49
  • Thank you, but the specific environment Im working in is for educational purposes and is from a book in the link I provided in the summary above. There is nothing for me to install. – Vili Jul 28 '17 at 13:08
  • follow tutorials point for nodeJs, there is online compiler provided. – Prasanna Jul 28 '17 at 13:34