can I declare a variable in javascript so $variable?

Asked

Viewed 182 times

4

I can declare a variable in javascript this way $variável ? If not. What would this syntax be ?

  • See also: https://answall.com/q/2513, https://answall.com/q/171327, https://answall.com/q/47165

2 answers

8


Briefly, you can, as long as you don’t need protection.

Javascript variables must meet the following requirements:

  • shall be composed of letters, numbers, dollar ($) or underscore (_)

  • should not start with numbers

    In addition, there is still the question of the scope:

  • declared variables without var or let have "superglobal" scope, sometimes even breaking the rules of the system and being used even before being declared

  • variables declared with var have global scope, and can be used anywhere in the script after being initialized

  • variables declared with let have block scope, that is, outside of your code block, they simply do not exist. They are the most recommended type, since they avoid errors of use and allow to clear the space in memory

  • type variables const have global scope and cannot be modified after creation (ideal for that const API = "192.168.10.12:8080/api/data" that will be used in 100% of the code)

In short, what you did was simply declare a superglobal variable.

4

No, this is the PHP variable creation syntax, in Javascript we declare variables using var, let or const, followed by the name, and the value:

var exemplo = "valor"

Of course the value is optional, in the example you gave ($variável) in Javascript would look like this: var variável only (placing ";" at the end of the expressions is also optional).

Another option is that you saw a superglobal variable. Superglobal and/or global variables are a big problem in Javascript (and in any language that is not ready for this), using var, let or const, you leave the local variable (if you declare it within a scope)

And last, you might have seen too: var $exemplo, that is, the $ in the variable name, and not being used in the declaration.

Variables can have a name beginning with "_" (underline), $, or a letter, and cannot start with numbers. You may also have seen a variable that has already been created being referenced.

  • But I’ve seen in Javascript this syntax $algumacoisa. I thought it could be variable the way it is in PHP. You know what that syntax could be? I saw this in JS ES6

  • So you probably saw: var $exemplo, i.e., the $ in the variable name, and not being used in the declaration, the variables may have the name started with "_" (underline), $, or a letter, and cannot start with numbers

  • You may also have seen a variable that has already been created being referenced

  • 2

    @yurishimoki can add these comments as a complement to the answer? actually in JS you can declare a variable $algumacoisa = 'foo';, This works in practice and your answer does not give to understand this nor why it would be "out of the norm" in JS.

  • I had forgotten hehe, now that I went to remember vlw

Browser other questions tagged

You are not signed in. Login or sign up in order to post.