answersLogoWhite

0

What is a javascript variable?

Updated: 8/11/2023
User Avatar

Wiki User

12y ago

Best Answer

A variable in JavaScript is an identifier that provides a storage location used to store a data value. The data stored at a particular location in memory. That we accessed by using the variable name.

In JavaScript programs, a script will have to store the bits of information temporarily. It needs to do its work. It can store this data in variables. In other words, A variable is a symbolic name given to a location in the computer memory. Where we store the value, which can be used in the program. The value of the variable can change during the execution of the program. It may take different values at different times during the execution of the program.

The name of the variable that we choose must be meaningful to understand, what it represents in the program.

For example

var Price = 100;

Rules to follow:

  • The name of the variable can contain a letter, digits, underscore(_), and dollar sign($).
  • They must begin with a letter, not with a digit.
  • Uppercase and Lowercase are distinct. That means the variable Total is not the same as total or TOTAl.
  • It should not be a keyword.
  • White space does also not allow.
  • Variable can be any length.

Hope this helps. Thank you

User Avatar

Shikha Sharma

Lvl 3
2y ago
This answer is:
User Avatar
User Avatar

Evie Stiedemann

Lvl 1
2y ago
awsome, ty
User Avatar

Lenore Murphy

Lvl 1
2y ago
great answer, tyyy!
More answers
User Avatar

Wiki User

10y ago

In most programming languages you have to specify the variable-type before you declare the variable, (String, int, double). But in javascript you just have to type var..

Example:

var variable1 = "Hello world"; String variable

var variable2 = 123; int variable

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Variables are stores of data. These take on several types, Integer, String and Array to name a few.

To declare a Variable simply type:

var THEVARNAME;

You can set the value right then and there:

var THEVARNAME = "helloworld";

And you can change it once declared.

THEVARNAME = "goodbye";

You can also detect whether it has been set in the scope (e.g. Locally and Globally)

If(!THEVARNAME){ alert("THEVARNAME not set"); } else { alert(THEVARNAME); }

Remember JavaScript is a Case Sensitive language. var, Var and VAR are not the same and Thevarname, TheVarName and THEVARNAME are not the same.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

variable are the containers for some value which can be further used for calculation and their values can be changed.

In javascript, variables are define as

var var_name;

It can be assigned to some value like as follow

var_name=10;

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

A Javascript variable is a letter or combination of letters, numbers, and symbols that a value is stored in. This value can be a string (words, letters), a boolean value (true/false), or a numerical value (125).

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Either:

function foo(){

//function body

}

or

var foo = function(){

//function body

}

The last example is preferable since it is consistent to creating properties to objects, even though they work more or less the same.

This answer is:
User Avatar

User Avatar

Shikha Sharma

Lvl 3
2y ago

A variable in JavaScript is an identifier that provides a storage location used to store a data value. The data stored at a particular location in memory. That we accessed by using the variable name.

In JavaScript programs, a script will have to store the bits of information temporarily. It needs to do its work. It can store this data in variables.

The name of the variable that we choose must be meaningful to understand, what it represents in the program.

For example:

var Price = 100;

You can use var and let Keyword declare variable in JavaScript.

Var Keyword:

Var keyword used to declare a variable like let do. It has function scoped, while the variable declared with var has global scope in the program.

Let Keyword:

Let keyword, used to declare a variable. With let, the scope of a variable has limited scope to the block where it declared. However, It can be only available inside the block where you declare it.

Hope this helps. Thank you

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

to set a variable it is just "var" then you have a space and give it the attributes that you want it to preform.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

A variable in JavaScript is declared with the varkeyword.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

var var1;
var var2= value;

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is a javascript variable?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is Undefined in javascript?

In JavaScript undefined is a special value used when a variable has not had a value set to it, or has not been declared.


How do you equate a php variable to a javascript variable?

Ideal thing would be to retrieve the value from PHP using AJAX and then assigning it to a java script variable. Thereafter compare it to the java script variable that is already present.


How can you use a JavaScript variable in the PHP isset function?

You can't actually do that. There is no direct way to make JavaScript code talk to PHP code, as the two languages are interpreted in different locations. The PHP is interpreted by the server, and the JavaScript is interpreted by the client. This means it's easy enough to transfer data from PHP to JavaScript (by generating the JavaScript with the PHP), but not the other way around. If you're simply looking for a way to see if a JavaScript variable is set (from within the JavaScript itself), that can be done with a line like this one: if(myVariable !== undefined){ /* do stuff */} If you actually want to handle it on the PHP side, one way to do so would be to use additional PHP code when that happens. For example: <?php $jsVars = array(); ?> <script type="text/javascript"> var foo = 'bar'; <?php $jsVars['foo'] = 'bar';?> var yub = 'nub'; <?php $jsVars['yub'] = 'nub';?> </script> ... You can then check to see whether a certain variable has been set by seeing if it's in that array: <?php function jsIsset($varname){ global $jsVars; return array_key_exists($varname, $jsVars); } ?> This however, only works when the JavaScript is generated, not when it's interpreted by the client system. For example, imagine you have a variable that is defined by a JavaScript function that is called from an onclick event. By the time that event happens, the page has already been served and the PHP is done executing. If you want the JavaScript to tell the PHP that a variable is defined, you would need to do it through an AJAX request, which I believe is beyond the scope of this question.


Why javascript function name and variable name same?

Either a case of bad programming practise or probably the function calls itself.


What is a JavaScript statement?

A statement in a programming language is a standalone unit of code. In JavaScript, statements generally end with a semicolon or a closing bracket.Examples:// variable statementvar x = 5;// if statementif (x == 0) {...}The ECMAScript language specification lists all the different types of statements permitted in JavaScript. See related links.

Related questions

What is Undefined in javascript?

In JavaScript undefined is a special value used when a variable has not had a value set to it, or has not been declared.


How do you put a JavaScript variable into the value of a HTML text box?

If your text box has the ID foo and your JavaScript variable is named bar, then you can use the following code to put the value of the variable into the text box:document.getElementById("foo").value=bar;


How can you declare the variable in PHP?

Variables in PHP do not need to be declared like some languages (e.g. JavaScript). This is all that needs to be done to assign a variable: $variable = "Value";


How does one use JavaScript arrays?

JavaScript arrays is used to generalize multiple values of data and store them in a single variable. This is a useful software in most programming languages.


How do you equate a php variable to a javascript variable?

Ideal thing would be to retrieve the value from PHP using AJAX and then assigning it to a java script variable. Thereafter compare it to the java script variable that is already present.


How do you assign a javascript variable to an object attribute?

You can assign a JavaScript variable to the attribute of an object using the assignment operator (=) Here's a quick example. var imageUrl = "image.png"; var img = new Image(); img.src = imageUrl; The above code would set the source attribute of the object named img as the contents of the variable imageUrl.


Can an expression be assigned variable ie expression equals variable?

Yes.Expressions are just javascript, so they can access any global variables on the page.The following works:var w = 10


How can you use a JavaScript variable in the PHP isset function?

You can't actually do that. There is no direct way to make JavaScript code talk to PHP code, as the two languages are interpreted in different locations. The PHP is interpreted by the server, and the JavaScript is interpreted by the client. This means it's easy enough to transfer data from PHP to JavaScript (by generating the JavaScript with the PHP), but not the other way around. If you're simply looking for a way to see if a JavaScript variable is set (from within the JavaScript itself), that can be done with a line like this one: if(myVariable !== undefined){ /* do stuff */} If you actually want to handle it on the PHP side, one way to do so would be to use additional PHP code when that happens. For example: <?php $jsVars = array(); ?> <script type="text/javascript"> var foo = 'bar'; <?php $jsVars['foo'] = 'bar';?> var yub = 'nub'; <?php $jsVars['yub'] = 'nub';?> </script> ... You can then check to see whether a certain variable has been set by seeing if it's in that array: <?php function jsIsset($varname){ global $jsVars; return array_key_exists($varname, $jsVars); } ?> This however, only works when the JavaScript is generated, not when it's interpreted by the client system. For example, imagine you have a variable that is defined by a JavaScript function that is called from an onclick event. By the time that event happens, the page has already been served and the PHP is done executing. If you want the JavaScript to tell the PHP that a variable is defined, you would need to do it through an AJAX request, which I believe is beyond the scope of this question.


How do you use equals equals equals operator in javascript?

'1' will return false because one variable is an integer and the other is a string.


Why javascript function name and variable name same?

Either a case of bad programming practise or probably the function calls itself.


What is a JavaScript statement?

A statement in a programming language is a standalone unit of code. In JavaScript, statements generally end with a semicolon or a closing bracket.Examples:// variable statementvar x = 5;// if statementif (x == 0) {...}The ECMAScript language specification lists all the different types of statements permitted in JavaScript. See related links.


How to convert a string into an integer in javascript?

In C# you can do that in the following way Convert.ToInt32(yourStringHere), for instance Convert.ToInt32("wikianswers"). But not all string can be converted to int type. If it happens compilator will throw the exception converting error which you can handle using structure try { ...//your code } catch (Exception) { ... //your code in the case of the exception }