Basic JavaScript Flashcards
Commenting
Used to leave notes
Code: // (for single line code) /* ... */ (for multi-line code)
Variables
- Store and manipulate data
- Store different values
- Name to represent data
- Declare a variable
Assignment operator
Code:
Stores a value to a variable.
Assigning value to another variable
-Use assignment operator
Code: var a; //no value a = 7; //has a value var b; //no value b = a; //now b = 7
Initialize a variable
-Initial value given to a variable
Code: var a = 9;
String literals
- i.e. = string
- Zero or more characters enclosed in single or double quotes
ex: “your name”
String variables
A string value assigned to a variable with single or double quotes.
var myFirstName = ‘Lucy’;
Uninitialized variables
- Would be ‘undefined’ initially if there is no assigned value.
- In a mathematical operation, the output would be ‘NaN’ (not a number)
Case sensitivity
- Use camelCase
- First letter of first word is lowercase and the first letter of all subsequent words are uppercase.
Code:
myFirstName
myLeastFavoriteAnimal
Var and let keywords
Var can be overwritten. Could be a problem.
Code: var camper = 'Fred'; var camper = 'James'' console.log(camper); //James will be printed
Let is better to use. The let variable can only be declared once, if you do not reassign it to a different value.
Const
- Read-only
- Cannot be reassigned
- Usually written in all caps with underscore for spacing
Number
-Data type which represents numeric data
Addition operator
”+”
Subtract operator
”-“
Multiply operator
“*”
Division operator
”/”
Increment a number
Code:
i++; (i = i +1)
Adds 1 to a number
Decrement a number
Code:
i–; (i = i - 1)
Create a decimal number
-Sometimes referred to as floating point numbers or floats
Remainder operator
Code:
“%”
const variable = 11 % 3; // Outputs 2
- Gives the remainder of the division of two numbers
- Can be used to check if a number is even or odd
Compound assignment (addition)
- (add this to the addition operator card = Everything to the right of the equals sign in evaluated first)
- Does the math operation and assignment in one step.
Code:
“+=”
let myVar = 1;
myVar += 5; // Outputs 6
Compound assignment (subtraction)
Code:
“-=”
Compound assignment (multiplication)
Code:
“*=”
Compound assignment (division)
Code:
“/=”
Escaping literal quotes in string
- Use this when you need a quote inside your quote.
- Escape a quote with “"
- Signifies that to JavaScript that it is not the end of the string
Code: const myStr = "I am a \"double quoted\" string inside \"double quotes\".";