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
“*”