Understand Variables & Data Types
Variables
let
In JavaScript the word let declares a variable. A variable is a reference to another piece of code. So, instead of typing out 'A teacher and web developer based in the SF Bay Area' you can use a reference like jeremyBio.
let jeremyBio = 'A teacher and web developer based in the SF Bay Area';console.log(jeremyBio);
Reserved Words
There are special words in JavaScript. These reserved also known as keywords tell are program to do something specific. For instance, one reserved words is typeof. Here is a list of other keywords.
Naming Variables
Naming Rules
Here are the rules for naming variables:
- Reserved words cannot be used as variable names.
- Variable names must begin with a letter, an underscore (_) or a dollar sign ($).
- Variable names can only contain letters, numbers, underscores, or dollar signs.
Naming Conventions
Camel Casing
A convention when naming variables is to use lower camel casing.
Camel casing is when each word is capitalized. For upper camel case the first word starts with a capital letter and for lower camel case the first word is lowercase. Typically when programmers just say camel case they are referring to lower camel case.
ThisIsWhatUpperCamelCaseLooksLikethisIsWhatLowerCamelCaseLooksLike
Good Variable Names
There are certain conventions for variables. Math variables for instance are often named x, y, z, a, or b. Variables that are repeated are often named i or j, which we will get to in the iteration section. Commonly variable names are a balance between being short and descriptive.
// Using math variableslet x = 3;let y = 2;let resultOfAdd = x + y;// Descriptive but shortlet favFruit = 'Mango';
Declaring Variables
To create a variable we use the let keyword followed by a space then the variable name. This is also known as declaring a variable.
let favPizza; // Declaring the variable favPizza
Assigning Variables
To tell the variable the piece of data it is referencing we use the equal sign.
let favPizza; // Declare the variablefavPizza = 'pepperoni'; // Assign the variable// We can also declare and assign the variable on the same linelet favGuitarist = 'Jimi Hendrix'; // Declare and assign the variable
Reassigning Variables
The reason variables are called variables is because you can change what the variable is referencing.
let favPizza = 'pepperoni'; // declare and assign the variableconsole.log(favPizza); // prints 'pepperoni'favPizza = 'mushroom'; // resassign the variableconsole.log(favPizza); // prints 'mushroom'
Data Types
We will be talking about three primitive data types: strings, numbers, and booleans.
Strings
Strings are typically words or phrases although a single character or several random characters put together also are be considered a string.
Strings start and end with a quote. The quote can be a single quote or a double quote, but the first quote and last quote must match.
'This is a proper string';"This is also a string";"This will not run because the quotes are not matching';
Numbers
Numbers are a second data type. These numeric values include numbers with and without decimals. Note that numbers wrapped in quotes would be considered part of the string data Types.
8; // a number8.342 // another number
Boolean
There are two boolean values - true and false.
This data type is useful for creating logical programs. Imagine you are checking to see if an email is valid. If there is an @ sign you may want to pass the user onto the next screen, but if there is not you may want to have the user check their email address.
true; // one of two boolean valuesfalse; // the second boolean value
typeof
The typeof operator tells you the data type. The syntax is either typing typeof then a space then your code you want check or you can wrap the code in parenthesis.
typeof true; // booleantypeof 9; // numbertypeof '9'; // stringtypeof 'true'; // stringtypeof 'Hellllo'; // stringtypeof(true); // boolean