How to access count? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to access count?

I've a variable named count in store.js set to 0. I want to access it index.js and then log its value. How do I do it?

12th May 2020, 4:37 PM
Sajid
Sajid - avatar
6 Answers
+ 2
Then store.js is unknown to your browser. You have to link a script to HTML page in order to run it. If you don't link it then it won't run and if it won't run variables or functions won't be created.
13th May 2020, 5:10 AM
Raj Chhatrala
Raj Chhatrala - avatar
+ 3
A variable that in in global scope should be accessible from any file. store.js: var count = 0; index.js: console.log(count); but you can also do module.exports from store.js and then import it in your index file.
12th May 2020, 4:48 PM
Raj Chhatrala
Raj Chhatrala - avatar
+ 2
Sajid Did you link your index.js before store.js? Then it will be of course undefined.
13th May 2020, 4:56 AM
Raj Chhatrala
Raj Chhatrala - avatar
0
🔫 Rick Grimes But I'm getting a error saying count is not defined 😥. Why is this happening!!!!
13th May 2020, 4:10 AM
Sajid
Sajid - avatar
0
🔫 Rick Grimes I didnt link store.js. I only linked index.js
13th May 2020, 5:03 AM
Sajid
Sajid - avatar
0
🔫 Rick Grimes thanks a lot. I've been stucked here for days. Now I understand why I was getting these stupid errors😡
13th May 2020, 5:21 AM
Sajid
Sajid - avatar