-
Notifications
You must be signed in to change notification settings - Fork 4
Variables & Constants
/*
* declare a variable called "name" and
* assign to it the String value of "John"
*/
var name = "John";
/*
* or within one statement, declare multiple variables
*/
var
nameFirst = "Jack",
nameLast = "Jones"
;
/*
* declare a constant, the value of which cannot be changed
*/
const birthPlace = "Timbuktu";Technically speaking, variables and constants are pointers to locations in memory where we've stored values.
When the JavaScript interpreter executes or runs a program, we can ask it at runtime to store a value by a name we provide, and by this, we can look up this value by its name to use at a later time.
Variables are so called because we can change the values to which they point. By contrast, the value of a constant can never change once assigned.
Contextually, you will come to understand when to use either a variable or constant. For example, while a person's birth date is constant, their age is not:
const birthDate = new Date(1987, 03, 07);
var age = 27;###Variables
Image I ask you, “Could you please hold this money in your pocket?”, and then I gave you some amount of money, which you place in your pocket. In this example, “money” is the name of the variable, and the “amount of money” is the value of that variable. Again, they are called variables because the value they point to can change, or vary - for example, if your pocket has a hole in it, the amount of money may not stay the same for very long.
var money = 10.00;
money -= 1.00;
console.log(money); // prints 9When we create variables, we use the var keyword to denote we are creating a variable, and often we assign a value to it at declaration, but not always, we may also declare the variable but not initialize it with any value, which means the container is created, but has no value, that is, it is undefined:
var money;
console.log(money); // prints undefinedAs best practice, it is often better to initialize variables at declaration, if you can. Consider this example:
var total = 0;
for (index in shoppingCart) {
total += shoppingCart[index].total;
}
console.log(total); // prints the total value of all items in the shoppingCart, and if none, prints 0By initializing total to 0, if there's no items in the shoppingCart, the value of total will default to zero, and therefore be immediately usable without having to check if it's defined. Use best practices where you can!
###Constants
When we create constants, we use the keyword const and assign a value to it immediately:
const birthPlace = "Timbuktu";
console.log(birthPlace); // prints TimbuktuIn the Node REPL, after initializing the constant birthPlace, if you tried to change the value, nothing would happen because once set, the value of a constant cannot change:
const birthPlace = "Timbuktu";
console.log(birthPlace); // prints Timbuktu
birthPlace = "North Bay";
console.log(birthPlace); // prints TimbuktuConstants are forever, while in scope!
###let
Beginning with JavaScript 1.7, the let keyword allows us to declare a block scope local variable, optionally initializing it to a value. Used inside a block, let limits the variable's scope to that block. See more on using the let here...
###Point to Any Type
Variables and constants can hold any type value, or any type of data - the possible datatypes in JavaScript are:
And while we're at it, variables and constants can also point to these types:
Variables and constants can also point to custom objects that YOU create!
Finally, note that JavaScript is a dynamic language and by default not statically typed, lacking compile-time ensurance of type safety preventing type conversion errors:
"...static typing means that programs are checked before being executed, and a program might be rejected before it starts. Dynamic typing means that the types of values are checked during execution, and a poorly typed operation might cause the program to halt or otherwise signal an error at run time. A primary reason for static typing is to rule out programs that might have such "dynamic type errors". - Norman Ramsey
What this means is that we can, for example, assign a the value variable to be a String and later assign to the same variable a value of a different type, like a Boolean, and the program will not crash:
var something = "It's a String";
console.log(something); // prints "It's a String"
something = true;
console.log(something); // prints trueThis means, of course, that you must know what you're assigning to your variables such that you don't experience unexpected results. Be vigilant!
© John Fraboni 2014