Js Variables Question | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Js Variables Question

Newbie here. Why is it that I don’t always need to type the var identifier when establishing a variable? Example: var test1 = 56 test2 = test1%5 == 0 ? 0 : 5-test1%5 console.log(test2) This will still put out the correct value of test2.

19th Jan 2022, 7:36 PM
Nicholas Miller
1 Answer
0
If you don't add "var", the variable gets declared as global. Note that declaring variables without var/let/const is highly discouraged. You can actually configure your editor to give you errors if you do it. ADVANCED: If you work with Node.js, you can add "use strict" to the top of the file you are working with, and it will force you to always declare in their local scope.
19th Jan 2022, 9:16 PM
Mohamad Kamar
Mohamad Kamar - avatar