0

Why is this simple global variable not working inside a function

var test_1 = "None";
$(function(){
    console.log(test_1)
})

I am getting the result Undefined

Santha Kumar
  • 83
  • 1
  • 12
  • Maybe you don't have jQuery? – Tap Jun 01 '18 at 00:48
  • but this is working: var test_1 = "None" console.log(test_1) – Santha Kumar Jun 01 '18 at 00:51
  • The $ variable will be equal to `jQuery` if the library is loaded on your page. Try this: `jQuery(function() { console.log(test_1); })`. If you get an error that jQuery is not defined, that's your problem. – Tap Jun 01 '18 at 00:54

1 Answers1

0

Check, whether you have jquery or not. I have created a jsfiddle (https://jsfiddle.net/2kudvsr0/) for your code and its printing None (desired output).

Just make sure you are executing your code once jQuery is imported successfully.

Vikas
  • 468
  • 3
  • 8