var myName = “Stanlee”;
In this example, var is understood as declaring a new variable, myName is what we will call that variable, = is telling the computer to store something, “Stanlee” is what the variable will store, and ; is like a period.
In general, there are three types of values a variable can be assigned to store.
A String is essentially text. When you are declaring a string, you have to put the desired text between quotations.
var string = “This is a String.”;
A Number is a numerical value. So, 8, 9, 25, 2354, or 4785 is considered a Number. No quotations are needed.
var number = 32547;
A Boolean is just a fancy term for true or false. They are often used to give a program parameters that allows it to respond to input from a user. A variable being assigned a boolean would look like:
var boolean = true;
So, with this, you now know how to create a new variable and what different kinds of values you can have that variable store. But, what good is this knowledge if you can’t see if it displayed? No good at all. The simplest way to have the computer display your variable in a pop-up box would be to type:
var show = alert(variable name);
This tells the computer to create a variable named ‘show’ that alerts the user to what is in the parentheses.
var example = “Hello World!”;
var showExample = alert(example);
Note: You can actually write a string, number, or boolean in the parentheses instead of a variable name, and the pop-up box will display that.
var display = alert(“I can write anything here!”);
Challenge of the week: If you created the variable,
var challenge = “This is a challenge!”;