0

Why in this example i get Uncaught ReferenceError: CONST1 is not defined

consts.js

const CONST1 = "hello";

sample.js

jQuery.getScript("consts.js', function() {
 alert(CONST1);
});

I guess it has something to do with scope. But since getScript uses global scope i cannot understand why this fails.

P.S. By using var instead of const it seems to work. But i cannot get the exact reason.

asked Jun 24, 2022 at 16:47
5
  • It is a scope issue. The same would occur if you used ESM or require Commented Jun 24, 2022 at 16:49
  • thans for the reply! Can you elaborate a little more? Isn't it applied in global scope? Also why var works? Commented Jun 24, 2022 at 17:25
  • <stackabuse.com/…> this article explains pretty well the scoping difference between var and const Commented Jun 24, 2022 at 18:37
  • for a better answer could u pls make a sample. Commented Jun 24, 2022 at 21:04
  • I have already provided some sample code. I can understand the difference between const and var. But in the given example I can't understand why const is not defined. Commented Jun 24, 2022 at 23:16

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.