Variables are used to store, change and access data. Each variable has its own name and holds a specific type of data.
When you declare a variable, you're stating that the identifier (the variable's name) will refer to a value of a specific data type.
let <variableName>: <type>;
Variable names have no spaces and can only use letters, numbers, and underscores.
If a variable name involves a phrase or multiple words, we often use something called camel case in which every word besides the first word is capitalized. This is not required but it can make your variable names easier to read.
Example of camel case:
let mybestfriend: string; //without camel case, this is ok let myBestFriend: string; //with camel case, this is better...preferred in fact!
No variable can be used before it is declared.
Example of declaration:
let myFavoriteNumber: number;
You cannot declare more than one variable with the same name within the same scope, and you cannot access a variable outside of its scope.
When you declare a variable without initializing it in the same statement, you must designate the variable's type.
When you assign a value to a variable for the first time, you are giving it an initial value. This is called initializing the variable. Here's what this looks like:
<variableName> = <value>;
The type of the value you assign to the variable must match the type you specified when you declared the variable.
Example of initialization:
let myFavoriteNumber: number; // declaration myFavoriteNumber = 101; // initialization
You may also declare and initialize a variable in the same statement. Here, designating a type is not necessary, since the program will be able to infer the type from the assignment.
Example of type inference:
let lameTeam = "Duke";
"Duke" is a string, therefore it is inferred that variable 'lameTeam' is a string without explicitly stating it!
After you declare and initialize a variable, you can still change its value. All you have to do is assign a new value to that variable. When you change a variable's value, you are reassigning a new value to that variable. A variable's value is the value most recently assigned to that variable.
Example with myFavoriteNumber:
let myFavoriteNumber = 110; // declaration + initialization print(myFavoriteNumber); //prints 110 myFavoriteNumber = 101; // myFavoriteNumber reassigned print(myFavoriteNumber); //prints 101 //note: myFavoriteNumber cannot be assigned to a value that's type is not 'number' //myFavoriteNumber = "Hello, World"; will result in an error
Keep in mind that the type of a variable cannot change after a type is declared/inferred!
You can access a variable's value by using the variable name.
Here are a few examples of making and using variables:
import { print } from "introcs";
let food: string; // declaration
food = "pizza"; // initialization
let numberOfSlicesLeft: number; // declaration is separate from initialization...type needed
numberOfSlicesLeft = 8; // initialization
let isHungry: = true; // declaration and initialization together...type inferred
numberOfSlicesLeft = 4; // reassignment
isHungry = false; // reassignment
print(food); // prints pizza
print(isHungry); // prints false
print(numberOfSlicesLeft); // prints 4