Basic Javascript Flashcards

1
Q

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 */
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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.
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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.
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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.
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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;

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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.
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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.
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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.
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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.
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

DECREMENT A NUMBER

You can easily decrement or decrease a variable by one with the – operator.

i–;
is the equivalent of

i = i - 1;
Note: The entire line becomes i–;, eliminating the need for the equal sign.

A
17
Q

CREATE DECIMAL NUMBERS

We can store decimal numbers in variables too. Decimal numbers are sometimes referred to as floating point numbers or floats.

Note: Not all real numbers can accurately be represented in floating point. This can lead to rounding errors. Details Here.

A
18
Q

MULTIPLY TWO DECIMALS

In JavaScript, you can also perform calculations with decimal numbers, just like whole numbers.

Let’s multiply two decimals together to get their product.

The product will equal 5.0.

const product = 2.0 * 2.5;

A
19
Q

DIVIDE DECIMAL

Now let’s divide one decimal by another.

const quotient = 4.4 / 2.2;

equals 2.2

A
20
Q

FINDING REMAINDER

The remainder operator % gives the remainder of the division of two numbers.

Example

5 % 2 = 1 because
Math.floor(5 / 2) = 2 (Quotient)
2 * 2 = 4
5 - 4 = 1 (Remainder)
Usage
In mathematics, a number can be checked to be even or odd by checking the remainder of the division of the number by 2.

17 % 2 = 1 (17 is Odd)
48 % 2 = 0 (48 is Even)
Note: The remainder operator is sometimes incorrectly referred to as the modulus operator. It is very similar to modulus, but does not work properly with negative numbers.

A
21
Q

COMPOUND ADDITION ASSIGNMENT AUGMENTED

In programming, it is common to use assignments to modify the contents of a variable. Remember that everything to the right of the equals sign is evaluated first, so we can say:

myVar = myVar + 5;
to add 5 to myVar. Since this is such a common pattern, there are operators which do both a mathematical operation and assignment in one step.

One such operator is the += operator.

let myVar = 1;
myVar += 5;
console.log(myVar);
6 would be displayed in the console.

A
22
Q

COMPOUND SUBTRACTION ASSIGNMENT AUGMENTED

Like the += operator, -= subtracts a number from a variable.

myVar = myVar - 5;
will subtract 5 from myVar. This can be rewritten as:

myVar -= 5;

A
23
Q

COMPOUND MULTIPLICATION ASSIGNMENT

The *= operator multiplies a variable by a number.

myVar = myVar * 5;
will multiply myVar by 5. This can be rewritten as:

myVar *= 5;

A
24
Q

COMPOUND DIVISION ASSIGNMENT AUGMENTED

The /= operator divides a variable by another number.

myVar = myVar / 5;
Will divide myVar by 5. This can be rewritten as:

myVar /= 5;

A
25
Q

ESCAPE LITERAL QUOTES IN STRINGS

When you are defining a string you must start and end with a single or double quote. What happens when you need a literal quote: “ or ‘ inside of your string?

In JavaScript, you can escape a quote from considering it as an end of string quote by placing a backslash () in front of the quote.

const sampleStr = "Alan said, \"Peter is learning JavaScript\".";
This signals to JavaScript that the following quote is not the end of the string, but should instead appear inside the string. So if you were to print this to the console, you would get:

Alan said, “Peter is learning JavaScript”.

A
26
Q

QUOTE STRINGS SINGLE QUOTES

String values in JavaScript may be written with single or double quotes, as long as you start and end with the same type of quote. Unlike some other programming languages, single and double quotes work the same in JavaScript.

const doubleQuoteStr = "This is a string"; 
const singleQuoteStr = 'This is also a string';
The reason why you might want to use one type of quote over the other is if you want to use both in a string. This might happen if you want to save a conversation in a string and have the conversation in quotes. Another use for it would be saving an <a> tag with various attributes in quotes, all within a string.
const conversation = 'Finn exclaims to Jake, "Algebraic!"';
However, this becomes a problem if you need to use the outermost quotes within it. Remember, a string has the same kind of quote at the beginning and end. But if you have that same quote somewhere in the middle, the string will stop early and throw an error.
const goodStr = 'Jake asks Finn, "Hey, let\'s go on an adventure?"'; 
const badStr = 'Finn responds, "Let's go!"';
Here badStr will throw an error.

In the goodStr above, you can use both quotes safely by using the backslash \ as an escape character.

Note: The backslash \ should not be confused with the forward slash /. They do not do the same thing.</a>

A
27
Q

ESCAPE SEQUENCES IN STRINGS

Quotes are not the only characters that can be escaped inside a string. There are two reasons to use escaping characters:

To allow you to use characters you may not otherwise be able to type out, such as a carriage return.
To allow you to represent multiple quotes in a string without JavaScript misinterpreting what you mean.
We learned this in the previous challenge.

Code	Output
\'	single quote
\"	double quote
\\	backslash
\n	newline
\r	carriage return
\t	tab
\b	word boundary
\f	form feed
Note that the backslash itself must be escaped in order to display as a backslash.
A
28
Q

CONCANTENATING WITH PLUS OPERATOR

In JavaScript, when the + operator is used with a String value, it is called the concatenation operator. You can build a new string out of other strings by concatenating them together.

Example

‘My name is Alan,’ + ‘ I concatenate.’
Note: Watch out for spaces. Concatenation does not add spaces between concatenated strings, so you’ll need to add them yourself.

Example:

const ourStr = "I come first. " + "I come second.";
The string I come first. I come second. would be displayed in the console.
A