Intro to JS Flashcards
Declare two variables: admin and name.
Assign the value “John” to name.
Copy the value from name to admin.
Show the value of admin using alert (must output “John”).
let admin, name; // can declare two variables at once
name = “John”;
admin = name;
alert( admin ); // “John”
let
is a modern variable declaration
var
is an old-school variable declaration. Normally we don’t use it at all, but has subtle differences from let
const
is like let, but the value of the variable can’t be changed
What are these called? ``
Backticks
assignment operator
You can assign a value using the assignment operator =. For example:
let hello = “Hello”;
7 primitive data types in JavaScript
String
Null
Undefined
Boolean
Number
BigInt
Symbol
String
a string represents a sequence of characters and can be enclosed in either single (‘) or double (“) quotes.
Note that strings are immutable, which means once they are created, they cannot be changed.
array
An array is a non-primitive data type that can hold a series of values.
Arrays are denoted using square brackets ([]). Here is an example of a variable with the value of an empty array:
let array = [];
Primitive Data Type
Primitive data types like strings and numbers can only hold one value at a time.
Non-Primitive Data Type
Non-primitive data types differ from primitive data types in that they can hold more complex data.
Declare a variable
Index
You can access the values inside an array using the index of the value. An index is a number representing the position of the value in the array, starting from 0 for the first value.
How would you console log the first item in an array?
console.log(arrayName[0]);
Mutable Arrays
Arrays are special in that they are considered mutable. This means you can change the value at an index directly.
For example, this code would assign the number 25 to the second element in the array:
let array = [1, 2, 3];
array[1] = 25;
Use the .length array to get the last value of an array
array[array.length - 1]
Method
A method in JavaScript is a function that’s associated with certain values or objects. An example you’ve already encountered is the .log() method, which is part of the console object.
Array .push()
This allows you to “push” a value to the end of an array.
arrayName.push(12);
Array .pop()
It removes the last element from an array and returns that element
popped = rows.pop();
” “ or ‘ ‘
Generally, it doesn’t matter, but you want to be consistent throughout the build
Const vs. Let
The let keyword allows a variable to be reassigned. This means you could change character later to be a completely different value.
First, a const variable cannot be reassigned like a let variable. This code would throw an error:
const firstName = “Naomi”;
firstName = “Jessica”;
A const variable also cannot be uninitialized. This code would throw an error:
const firstName;
Iterator
The iterator is a variable you can declare specifically in your for loop to control how the loop iterates or goes through your logic.
It is a common convention to use i as your iterator variable in a loop.
for loop syntax
for (let i = 0; i < count; i = i + 1) {
}
concatenation