JavaScript Variables
- Like nouns in the English language, JavaScript uses variables. Variables hold values of different types: integers, words, etc.
- JavaScript treats all integers and floats the same.
- Variables can save values as a string (“hi I am a string 123”), integer (19), boolean, null or undefined.
DECLARING / INITIALIZING A VARIABLE:
• Declaring a variable does not necessarily mean that you are assigning it a value but it will be the first time a variable is mentioned in a script. If it is not declared with a given value it will take on an automatic value that is usually “undefined.”
• When you assign a variable for the first time, it is called initializing it. Think of it as when you assign the ‘initial’ value.
• Thereafter, any other time you reassign a variable is just called assigning a value.
ASSIGNING A VARIABLE:
• Assigning a value is any time you give it a value. This includes the initial assignment which is also called initializing. Any let variable can be reassigned as many times as necessary.
- Start with an introductory
- Follow with variable’s name in camelCase
- = // a single = sign assigns a value
- Value comes after the = and defines what the variable represents. Values can begin empty, as null
Keywords
- Keywords are required when declaring new variables.
- There are three basic keywords that are used: const, let and var.
- When variables are called upon later in the script, you should not use a keyword again. Neither are keywords necessary in reassigning a variable with a new value. Only that first declaration/initiation.
COMMON INTRODUCTORY KEYWORDS:
• const short for “constant” specifies that the data will not change.
const myHometown = “Haiku, Hawaii”;
• let specifies that the value may change and we will ‘let’ it change throughout the course of a program.
let myAge = 29;
• var the oldest way of declaring a variable, not as common these days but important to know what it is because it does show up here and there.