Category: Variables
Declares a variable with the given name.
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.
// Pioneering computer scientist. var name; var birthYear; name = "Alan Turing"; birthYear = 1912; console.log(name + " was born in the year " + birthYear);
Example: Count Sixes Count the number of sixes rolled in 5 random die rolls.
// Count the number of sixes rolled in 5 random die rolls. var counter; counter=0; if (randomNumber(1,6)==6) counter = counter + 1; if (randomNumber(1,6)==6) counter = counter + 1; if (randomNumber(1,6)==6) counter = counter + 1; if (randomNumber(1,6)==6) counter = counter + 1; if (randomNumber(1,6)==6) counter = counter + 1; console.log("You threw " + counter + " sixes out of 5 throws of a die.");
Example: Simple Average Average three tempertatures prompted from the user.
// Average three tempertatures prompted from the user. var temperatureSum; temperatureSum=0; temperatureSum = temperatureSum + promptNum("Enter the first temperature"); temperatureSum = temperatureSum + promptNum("Enter the second temperature"); temperatureSum = temperatureSum + promptNum("Enter the third temperature"); console.log ("The average temperature is " + temperatureSum/3 );
var x;
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 _. |
No return value. Variable created in memory.
Found a bug in the documentation? Let us know at documentation@code.org
var x;
Does not return anything. someVal
created in memory.
Found a bug in the documentation? Let us know at documentation@code.org