Basic JavaScript Flashcards

learn the basics

1
Q
  1. Comment Your JavaScript Code:

How do you comment a single line?

How do you comment multiple lines?

A

//

/………………./

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
  1. Declare JavaScript Variables:

What are the 7 data types?

What can variable names be made up of?

What can variable names not contain or start with?

A

undefined, null, boolean, string, symbol, number and object.

Variable names can be made up of numbers, letters, and $ or _,

Variable names cannot contain spaces or start with a number.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
  1. Storing Values with the Assignment Operator:

How do you assign a variable?

Which way does assignment go?

A

myVariable = 5;
This assigns the Number value 5 to myVariable.

Assignment always goes from right to left.
Everything to the right of the = operator is resolved before the value is assigned to the variable to the left of the operator.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  1. Initializing Variables with the Assignment Operator:

How do you assign the value of 0 to myVar?

A

var myVar = 0;

Creates a new variable called myVar and assigns it an initial value of 0.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
  1. Understanding Uninitialized Variables:

What is the initial value of variables when they are declared?

What happens when you do a mathematical operation on an undefined variable?

What happens when you concatenate a string with an undefined variable?

A

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 literal string of “undefined”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
  1. Understanding Case Sensitivity in Variables:

MYVAR not the same as MyVar or myvar

What is camelCase?

A

var myVar;

var someVariable;

var anotherVariableName;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
  1. Add Two Numbers with JavaScript:

Number is a data type in JavaScript which represents numeric data.

How do you assign a variable that adds 5 + 10?

A

myVar = 5 + 10; // assigned 15

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
  1. Subtract One Number from Another with JavaScript:

How do you assign a variable that subtracts 6 from 12?

A

myVar = 12 - 6; // assigned 6

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
  1. Multiply Two Numbers with JavaScript:

How do you assign a variable that multiplies 13 by 13?

A

myVar = 13 * 13; // assigned 169

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
  1. Divide One Number by Another with JavaScript:

How do you assign a variable the divides 16 by 2?

A

myVar = 16 / 2; // assigned 8

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
  1. Increment a Number with JavaScript:

You can easily increment or add one to a variable with the ++ operator.

What is the equivalent of i = i + 1;?

A

i++;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
  1. Decrement a Number with JavaScript:

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

What is the equivalent of i = i - 1;?

A

i–;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
  1. Create Decimal Numbers with JavaScript:

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

Create a variable myDecimal and give it a decimal value with a fractional part (e.g. 5.7).

A

var myDecimal = 5.7;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
  1. Multiply Two Decimals with JavaScript:

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

Multiply two decimals together to get their product?

A

var product = 2.0 * 0.0;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
  1. Divide One Decimal by Another with JavaScript:

Change the first decimal so that the quotient will equal to 2.2.?

A

var quotient = 4.4 / 2.0;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
  1. Finding a Remainder in JavaScript:

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

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.

What does 5 % 2 = ?

A

5 % 2 = 1

17
Q
  1. Compound Assignment With Augmented Addition:

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.

var myVar = 1;
myVar += 5;
console.log(myVar);?

A

// Returns 6

18
Q
  1. Compound Assignment With Augmented Subtraction:

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

myVar = myVar - 5;

will subtract 5 from myVar. This can be rewritten as?

A

myVar -= 5;

19
Q
  1. Compound Assignment With Augmented Multiplication:

The *= operator multiplies a variable by a number.

myVar = myVar * 5;

will multiply myVar by 5. This can be rewritten as?

A

myVar *= 5;

20
Q
  1. Compound Assignment With Augmented Division:

The /= operator divides a variable by another number.

myVar = myVar / 5;

Will divide myVar by 5. This can be rewritten as?

A

myVar /= 5;

21
Q
  1. Declare String Variables:

Previously we have used the code

var myName = “your name”;

“your name” is called a string literal. It is a string because it is a series of zero or more characters enclosed in single or double quotes.

How do you create two new string variables: myFirstName and myLastName and assign them the values of your first and last name, respectively?

A
var firstName = "George";
var lastName = "Rodriguez";
22
Q
  1. Escaping 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.

A

var 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”.

23
Q
  1. Quoting Strings with 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.

doubleQuoteStr = "This is a string"; 
singleQuoteStr = 'This is also a string';
A

goodStr = ‘Jake asks Finn, “Hey, let's go on an adventure?”’;

badStr = ‘Finn responds, “Let’s go!”’; // Throws an error

24
Q
  1. Escape Sequences in Strings:
Code	Output
\'	single quote
\"	double quote
\\	backslash
\n	newline
\r	carriage return
\t	tab
\b	backspace
\f	form feed

What are 2 reasons to use escaping characters?

A

Quotes are not the only characters that can be escaped inside a string.

There are two reasons to use escaping characters:

First is to allow you to use characters you might not otherwise be able to type out, such as a backspace.

Second is to allow you to represent multiple quotes in a string without JavaScript misinterpreting what you mean.

25
Q
  1. Concatenating Strings 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.

Watch out for spaces. Concatenation does not add spaces between concatenated strings, so you’ll need to add them yourself.

Build myStr from the strings “This is the start. “ and “This is the end.” using the + operator.

A

var myStr = “This is the start. “ + “This is the end.”;

26
Q
  1. Concatenating Strings with the Plus Equals Operator:

We can also use the += operator to concatenate a string onto the end of an existing string variable. This can be very helpful to break a long string over several lines.

A
// Example
var ourStr = "I come first. ";
ourStr += "I come second.";
27
Q
  1. Constructing Strings with Variables:

Sometimes you will need to build a string, Mad Libs style. By using the concatenation operator (+), you can insert one or more variables into a string you’re building.

A
// Example
var ourName = "freeCodeCamp";
var ourStr = "Hello, our name is " + ourName + ", how are you?";
28
Q
  1. Appending Variables to Strings:

Just as we can build a string over multiple lines out of string literals, we can also append variables to a string using the plus equals (+=) operator.

A
// Example
var anAdjective = "awesome!";
var ourStr = "freeCodeCamp is ";
ourStr += anAdjective;
29
Q
  1. Find the Length of a String:

You can find the length of a String value by writing .length after the string variable or string literal.

“Alan Peter”.length; // 10

A
// Example
var firstNameLength = 0;
var firstName = "Ada";

firstNameLength = firstName.length;

30
Q
  1. Use Bracket Notation to Find the First Character in a String:

Bracket notation is a way to get a character at a specific index within a string.

Most modern programming languages, like JavaScript, don’t start counting at 1 like humans do. They start at 0. This is referred to as Zero-based indexing.

For example, the character at index 0 in the word “Charles” is “C”. So if var firstName = “Charles”, you can get the value of the first letter of the string by using firstName[0].

A
// Example
var firstLetterOfFirstName = "";
var firstName = "Ada";

firstLetterOfFirstName = firstName[0];