Variables
In programming, a variable is a container for a value. You can think of variables as little containers for information that live in a computer’s memory. Information stored in variables, such as a username, account number, or even personalized greeting can then be found in memory.
There are a few general rules for naming variables:
- Variable names cannot start with numbers.
- Variable names are case sensitive, so
myName
and myname
would be different variables. It is bad practice to create two variables that have the same name using different cases.
- Variable names cannot be the same as keywords.
The
var
, short for variable, is a JavaScript keyword that creates, or declares, a new variable.
- The
let
keyword signals that the variable can be reassigned a different value.
- The way you declare a
const
variable and assign a value to it follows the same structure as let
and var
. A const
variable cannot be reassigned because it is constant.
Mathematical work and symbols:
levelUp += 5;
powerLevel -= 100;
multiplyMe *= 11;
quarterMe /= 4;
-
Other mathematical assignment operators include the increment operator (
++
) and decrement operator (--
).
EX: increase the value of gainedDollar
.
gainedDollar++;
myName
and myname
would be different variables. It is bad practice to create two variables that have the same name using different cases.The
var
, short for variable, is a JavaScript keyword that creates, or declares, a new variable.let
keyword signals that the variable can be reassigned a different value. const
variable and assign a value to it follows the same structure as let
and var
. A const
variable cannot be reassigned because it is constant.
levelUp += 5;
powerLevel -= 100;
multiplyMe *= 11;
quarterMe /= 4;
- Other mathematical assignment operators include the increment operator (
++
) and decrement operator (--
).
EX: increase the value of
gainedDollar
.
gainedDollar++;
String Concatenation with Variables
EX: Use
console.log()
to print 'My favorite animal: ANIMAL'
to the console. Use string concatenation so that ANIMAL
is replaced with the value in your favoriteAnimal
variable.
let favoriteAnimal = "giraffe";
console.log("My favorite animal: " + favoriteAnimal);
String Interpolation
A template literal is wrapped by backticks
`
(this key is usually located on the top of your keyboard, left of the 1 key).
Inside the template literal, you’ll see a placeholder,
${myPet}
. The value of myPet
is inserted into the template literal.
When we interpolate
`I own a pet ${myPet}.`
, the output we print is the string: 'I own a pet armadillo.'
No comments:
Post a Comment