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;