Basic Javascript Flashcards
COMMENTING CODE
Using // will tell JavaScript to ignore the remainder of the text on the current line. This is an in-line comment:
// This is an in-line comment. You can make a multi-line comment beginning with /* and ending with */. This is a multi-line comment:
/* This is a multi-line comment */
DECLARING VARIABLES
In computer science, data is anything that is meaningful to the computer. JavaScript provides eight different data types which are undefined, null, boolean, string, symbol, bigint, number, and object.
For example, computers distinguish between numbers, such as the number 12, and strings, such as “12”, “dog”, or “123 cats”, which are collections of characters. Computers can perform mathematical operations on a number, but not on a string.
Variables allow computers to store and manipulate data in a dynamic fashion. They do this by using a “label” to point to the data rather than using the data itself. Any of the eight data types may be stored in a variable.
Variables are similar to the x and y variables you use in mathematics, which means they’re a simple name to represent the data we want to refer to. Computer variables differ from mathematical variables in that they can store different values at different times.
We tell JavaScript to create or declare a variable by putting the keyword var in front of it, like so:
var ourName;
In JavaScript we end statements with semicolons. Variable names can be made up of numbers, letters, and $ or _, but may not contain spaces or start with a number.
STORING VALUES WITH THE ASSIGNMENT OPERATOR
In JavaScript, you can store a value in a variable with the assignment operator (=).
myVariable = 5;
This assigns the Number value 5 to myVariable.
If there are any calculations to the right of the = operator, those are performed before the value is assigned to the variable on the left of the operator.
var myVar;
myVar = 5;
First, this code creates a variable named myVar. Then, the code assigns 5 to myVar. Now, if myVar appears again in the code, the program will treat it as if it is 5.
ASSIGN THE VALUE OF ONE VARIABLE TO ANOTHER
After a value is assigned to a variable using the assignment operator, you can assign the value of that variable to another variable using the assignment operator.
var myVar; myVar = 5; var myNum; myNum = myVar; The above declares a myVar variable with no value, then assigns it the value 5. Next, a variable named myNum is declared with no value. Then, the contents of myVar (which is 5) is assigned to the variable myNum. Now, myNum also has the value of 5.
INITIALIZING VARIABLES WITH THE ASSIGNMENT OPERATOR
It is common to initialize a variable to an initial value in the same line as it is declared.
var myVar = 0; Creates a new variable called myVar and assigns it an initial value of 0.
DECLARE STRING VARIABLES
Previously you used the following code to declare a variable:
var myName;
But you can also declare a string variable like this:
var myName = "your name"; "your name" is called a string literal. A string literal, or string, is a series of zero or more characters enclosed in single or double quotes.
UNDERSTANDING UNINITIALIZED VARIABLES
When JavaScript variables are declared, they have an initial value of undefined. If you do a mathematical operation on an undefined variable your result will be NaN which means “Not a Number”. If you concatenate a string with an undefined variable, you will get a string of undefined.
UNDERSTANDING CASE SENSITIVITY IN VARIABLES
In JavaScript all variables and function names are case sensitive. This means that capitalization matters.
MYVAR is not the same as MyVar nor myvar. It is possible to have multiple distinct variables with the same name but different casing. It is strongly recommended that for the sake of clarity, you do not use this language feature.
Best Practice
Write variable names in JavaScript in camelCase. In camelCase, multi-word variable names have the first word in lowercase and the first letter of each subsequent word is capitalized.
Examples:
var someVariable;
var anotherVariableName;
var thisVariableNameIsSoLong;
DIFFERENCES BETWEEN VAR AND LET
One of the biggest problems with declaring variables with the var keyword is that you can easily overwrite variable declarations:
var camper = "James"; var camper = "David"; console.log(camper); In the code above, the camper variable is originally declared as James, and is then overridden to be David. The console then displays the string David.
In a small application, you might not run into this type of problem. But as your codebase becomes larger, you might accidentally overwrite a variable that you did not intend to. Because this behavior does not throw an error, searching for and fixing bugs becomes more difficult.
A keyword called let was introduced in ES6, a major update to JavaScript, to solve this potential issue with the var keyword. You’ll learn about other ES6 features in later challenges.
If you replace var with let in the code above, it results in an error:
let camper = "James"; let camper = "David"; The error can be seen in your browser console.
So unlike var, when you use let, a variable with the same name can only be declared once.
DECLARE READ-ONLY VARIABLES WITH CONST
The keyword let is not the only new way to declare variables. In ES6, you can also declare variables using the const keyword.
const has all the awesome features that let has, with the added bonus that variables declared using const are read-only. They are a constant value, which means that once a variable is assigned with const, it cannot be reassigned:
const FAV_PET = "Cats"; FAV_PET = "Dogs"; The console will display an error due to reassigning the value of FAV_PET.
You should always name variables you don’t want to reassign using the const keyword. This helps when you accidentally attempt to reassign a variable that is meant to stay constant.
Note: It is common for developers to use uppercase variable identifiers for immutable values and lowercase or camelCase for mutable values (objects and arrays). You will learn more about objects, arrays, and immutable and mutable values in later challenges. Also in later challenges, you will see examples of uppercase, lowercase, or camelCase variable identifiers.
ADD TWO NUMBERS WITH JAVASCRIPT
Number is a data type in JavaScript which represents numeric data.
Now let’s try to add two numbers using JavaScript.
JavaScript uses the + symbol as an addition operator when placed between two numbers.
Example:
const myVar = 5 + 10; myVar now has the value 15.
SUBTRACT NUMBERS WITH JAVASCRIPT
We can also subtract one number from another.
JavaScript uses the - symbol for subtraction.
Example
const myVar = 12 - 6; myVar would have the value 6.
MULTIPLY TWO NUMBERS WITH JAVASCRIPT
We can also multiply one number by another.
JavaScript uses the * symbol for multiplication of two numbers.
Example
const myVar = 13 * 13; myVar would have the value 169.
DIVIDE TWO NUMBERS WITH JAVASCRIPT
We can also divide one number by another.
JavaScript uses the / symbol for division.
Example
const myVar = 16 / 2; myVar now has the value 8.
INCREMENT A NUMBER
You can easily increment or add one to a variable with the ++ operator.
i++;
is the equivalent of
i = i + 1;
Note: The entire line becomes i++;, eliminating the need for the equal sign.