variables

In this module we'll study variables.

Description

Think of a variable as a container. Cup analogies work really well with understanding variables. They store info. We can pour liquid into a cup and change it's value. We can pour two half cups into another cup. We can pour one cup into a bigger cup and then put it into an array of cups.

File Location

You should be located in the following file:

   javascript-library
        └── 0-PreWork
            └── 1-HTML-Basics
            └── 2-CSS-Basics
            └── 3-JavaScript-Basics
                02-variables.js       <----You will be working in this file in this module.

Description

Variables are used to store values. They are containers for values. That's it. Let's keep it that simple for now.

Declarations

First, they must be declared:

    var x;

The name could be anything you want. You could name it spaghetti or football.

Initialized

Then, variables must be initalized:

    x = 5;

Initialization is used for stating what the value of that variable is holding, as well as its type. (More on types later).

Declarations and Initializations

You can also do it all in one statement:

    var theNumberFive = 5;

Variables Demo

Variables Demo

var x = 1;
console.log(x);

Assignment

It is common to assign a value to a variable. Then use that variable to stand for that think: Consider the following:

const feetInMile = 5260;
console.log(feetInMile);

Naming

We can name variables anything we want. This means that there is room for creative writing in software:

var ageOfMyOldestFerret = 40;
var numberOfEggsInOneSitting = 50;

The variables should be sensibly named though, and able to be understood by fellow developers.

Camel Casing

Variable names, in most cases, should be camel cased. This means that the first word is lower case and each consecutive word is upper case. It has humps like a camel. Consider the following examples:

var numberOfGrammysIHaveWon = 0; 
var myBirthYear = 1976;
var newNumber = 1 * numberOfGrammysIHaveWon;

Think it Through

Think through the answer for each one of the problems below:

var age = 40;
var ageInAugust = age + 1;  
console.log(ageInAugust);//What will you get here?

var eleven = 11;
console.log(eleven * eleven); //What will the answer be?

var nineteen = 19;
console.log(age - nineteen); //What will the answer be?

A FEW DON'TS

You can't start a variable with a number:

var 2nite = "Tonight"
//console.log(2nite); //won't work.

Reserved Words

There are exceptions for naming variables. The following list is a group of reserved words in JavaScript, and they should not be used as variable names:

Reserved Word List

abstract

else

instanceof

super

boolean

enum

int

switch

break

export

interface

synchronized

byte

extends

let

this

case

false

long

throw

catch

final

native

throws

char

finally

new

transient

class

float

null

true

const

for

package

try

continue

function

private

typeof

debugger

goto

protected

var

default

if

public

void

delete

implements

return

volatile

do

import

short

while

double

in

static

with

Last updated