Declare and assign a value to a variable

Category:Variables

Declare and assign a value to a variable

Category: Variables

Declares and assigns an initial value to a variable.

Many apps process data. To be able to process data your apps need to keep track of the data in memory. Variables are simply names you use to refer to stored data in your apps. You can name your variables whatever you want so long as the name is not already used by the system. Variable names can not have spaces or special characters. In practice, it is helpful to name your variables in a way that describes the value they store. For instance, if the variable you create is to store a person's name you might name that variable personName.

To process data in our apps we need to assign values to memory locations we have previously named using var to declare a variable. Programmers read the statement "area = length * width;" as "area gets length times width". The variable getting the value always goes on the left hand side of the assignment operator =. The right hand side of the assignment operator can be a number or a string, or the number or string returned by a function, or the numeric or string result of the evaluation of an expression.

Examples


// Pioneering computer scientist and first African American to earn a Ph.D. in computer science.
var name = "Marsha Rhea Williams";
var birthYear = 1948;
console.log(name + " was born in the year " + birthYear);

Example: Unbeatable Coin Flip Generate a random 1 or 2, and always win because of carefully worded messages.

// Generate a random 1 or 2, and always win because of carefully worded messages.
var myRandomNumber = randomNumber(1,2);
if(myRandomNumber == 1){
  console.log("Heads, I win!");
}
else {
  console.log("Tails, you lose!");
}

Syntax

var x = ___;

Parameters

Name Type Required? Description
x variable name Yes The name you will use in the program to reference the variable. Must begin with a letter, contain no spaces, and may contain letters, digits, - and _.
___ any type Yes The right hand side of the assignment operator can be a number or a string, or the number or string returned by a function, or the numeric or string result of the evaluation of an expression.

Returns

No return value. Variable created in memory and value assigned.

Tips

  • You don't strictly need to provide a variable with an initial value when you create it, but it is a good practice, because if you accidentally use a variable that has never been assigned a value you can get unpredictable results in your code.
  • Variables can store numbers, strings, arrays or objects.
  • The block of code where you declare the variable defines the variable's scope. Scope refers to which blocks of code can access that variable by name. For instance, if you declare a variable inside a function, that variable name can only be accessed inside that function. Variables decalred at the top of your program are global and can be accessed anywhere in your program.
  • = is the assignment operator. == is the boolean check for equivalency operator.

Found a bug in the documentation? Let us know at documentation@code.org

Syntax

var x = __;

Found a bug in the documentation? Let us know at documentation@code.org