-3

I am new to JS. I have the following question -

Is it right to say that I can declare variables only in global scope or inside a function/method only in JS and in no other type of Object which is not function/method?

I understand that properties can be defined in any object of Javascript but variables cannot be declared everywhere.


EDIT: I might not have right terminology to explain but in simplest words I need a list of places where i can write var x = .. in JS code. Please,I am not looking for scope of a variable.

Number945
  • 4,631
  • 8
  • 45
  • 83
  • Variables using `var` have function-scope. Variables using `const` or `let` have block-scope. It's recommended to use the most specific scope necessary to accomplish your task to avoid name-clashes and possibility of global overrides. – Sunny Patel Jan 13 '20 at 21:00
  • 1
    Yes, by definition, JS variables are function-scoped. Meaning, they have scope within the function in which they are defined. With the introduction of the `let` and `const` keywords in ES2015, variables may be scoped within a specific block, but that block will still be inside a function or global scope. – mhodges Jan 13 '20 at 21:01
  • @mhodges , So my observation is right ? – Number945 Jan 13 '20 at 21:02
  • @BreakingBenjamin I believe so, yes. I'm trying to think of where else you could define a variable- do you have examples from other languages? – mhodges Jan 13 '20 at 21:03
  • https://jsbin.com/wigumuneze/edit?js,console – Quentin Jan 13 '20 at 21:04
  • 1
    @Quentin That's still defining the variable within the global scope. Unless the OP is only asking about where the variable declaration is made? – mhodges Jan 13 '20 at 21:06
  • 2
    @SunnyPatel I think the question is more about syntax than scope. – Barmar Jan 13 '20 at 21:06
  • @mhodges — It sounds like they are asking where the declaration can be made. It's not very clear – Quentin Jan 13 '20 at 21:09
  • @Quentin Agreed, it's really unclear. OP Can you please clarify? – mhodges Jan 13 '20 at 21:10
  • @mhodges I wanted the places where I can write `var x =..` (in simplest words) – Number945 Jan 13 '20 at 21:10
  • @BreakingBenjamin Where you can use the `var` keyword, specifically? Or where you can declare a variable? Believe it or not, they are not the same thing, as Quentin's example shows quite well. – mhodges Jan 13 '20 at 21:12
  • @mhodges — They aren't. Stick a `var` before the implicit global declaration in that example and it'll error. – Quentin Jan 13 '20 at 21:13
  • @BreakingBenjamin You can use var a = ... everywhere you want, just if you do it globlal that variable could be used on all the functions you have, if you declare a variable inside a function this variable is going to be only used inside that function – User1899289003 Jan 13 '20 at 21:13
  • @mhodges example I cannot do inside an non function object like `var obj = { // cant do here directly}` – Number945 Jan 13 '20 at 21:14
  • @BreakingBenjamin Correct, but you could, however, define one in a `for` loop statement, like so: `for (var i = 0; i < 5; i++) {...}` Technically it's still within the scope of the containing block/funciton, so my above statement still stands. Using the `let` keyword would contain the variable scope to the block for that given iteration of the for loop. – mhodges Jan 13 '20 at 21:16
  • @mhodges correct. Thats why upvoted your comment.I think answer posted by Barmar is also correct. – Number945 Jan 13 '20 at 21:17

2 Answers2

-1

you can the variable anywhere you want at the beggining of the program in the middle or if you need one in a function you can do it there too

Glide
  • 17
  • 1
-2

Variables can be declared at top-level, function parameter lists, and statements in function bodies.

Object and array literals can contain nested functions, and you can declare variables inside these functions as well. But you can't declare variables directly in object/array literals; e.g. you can't write:

const foo_array = [
    let a = 1,
    let b = 2
];

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Can u pls give an example how to do that in `statements in class definitions` ? – Number945 Jan 13 '20 at 21:19
  • 1
    That was wrong, a statement that looks like a variable assignment in a class definition is actually a property in the class prototype, not really a variable. I've removed it. – Barmar Jan 13 '20 at 21:25
  • I guess we need to use `declare` rather than `define` keyword. This was the whole fuss in the comments. https://stackoverflow.com/a/20822138/2844702 – Number945 Jan 13 '20 at 21:39
  • 1
    @BreakingBenjamin I've changed to `declare`, although I think for this question the distinction is not really significant. A definition is just a declaration with an initialization. – Barmar Jan 13 '20 at 21:42