Arrays
Definition: Array is a term for a list in JavaScript
Syntax: Each item surrounded by a quotation and group surrounded by bracket separated with commas.
Example: [‘Item 1’, ‘Item 2’, ‘Item 3’]
Variables
Definition: Variables is a container that stores information
Syntax: start with var, then variable name, =, then the value to set variable to
Example: var todos = ['Item 1', 'Item 2', 'Item 3']
console.log() Function
console.log() – Prints to console. Can take multiple items.
Example:
var todos = ['Item 1', 'Item 2', 'Item 3']
console.log("My ToDos:", todos)
= My ToDos: ["item 1", "item 2", "item 3"]
Adding Array Items
.push() Function
.push() – command to add items to an array. Think of it as pushing to (adding to the end of) the array. Returns new number of items in the array
Example: todos.push(‘item 4’)
= 4
console.log(“My ToDos:”, todos)
= My ToDos [“item 1”, “item 2”, “item 3” ‘item 4’]
Calling Array Items
Call a array item – to call a specific item in an array use brackets with a number within. Important, the first item is 0, second item is 1, ect.
Syntax: varName[number]
Example:
todos[0]
= item 1
Changing Array Items
Change an array item – to change an array i
Syntax: varName[number] = ‘new value’
Example: todos[0] = ‘item 1 updated’
= “item 1 updated”
Deleting Array Items
.splice() – Deletes array items. requires first item to delete and how many items. Returns the deleted item. If [] is returned, the command wasnt able to delete anything.
Syntax: array.splice(firstItem, numItems)
Example:
todos.splice(0, 1) – deletes first item