Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (13.1k points)
Can anyone help me how I can able to define global variable in a JavaScript function?

1 Answer

0 votes
by (26.7k points)

There is multiple ways to set the variable as a global in JavaScript, you can use var at global scope so that it will become a global variable.

<script>

var yourGlobalVariable;

function foo() {

    // ...

}

</script>

Also, you can use globalThis, if you are using any modern environments:

<script>

function foo() {

    globalThis.yourGlobalVariable = ...;

}

</script>

I hope this will help.

Want to know more about Java? Prefer this tutorial on Learn Java.

Want to become a Java expert? Join Java Course now!!

Related questions

0 votes
1 answer
asked Apr 8, 2021 in Java by dante07 (13.1k points)
0 votes
1 answer
0 votes
1 answer
asked Apr 19, 2021 in Java by sheela_singh (9.5k points)
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...