Skip to content

Instantly share code, notes, and snippets.

View darleykrefta's full-sized avatar
🎯
Focusing

Darley Krefta darleykrefta

🎯
Focusing
View GitHub Profile
const name = 'Han'

// define fullName with name value
let fullName = name

// change fullName value
fullName = fullName + ' Solo'
const droid = {
  name: 'R2-D2',
  quality: 'resourceful'
}

// define newDroid with droid reference
const newDroid = droid

// update some attribute
const ships = ['X-wing', 'TIE Fighter', 'Millenium Falcon']

// define newShips with ships reference
const newShips = ships

// push 'Destroyer' value to new array created
newShips.push('Destroyer')

// the original variable is updated too, because it is the original reference
const ships = ['X-wing', 'TIE Fighter', 'Millenium Falcon']

// define newShips with a spread of ships, creating a new reference
const newShips = [...ships, 'Destroyer']

// the original variable wasn't updated
console.log(ships)
// ['X-wing', 'TIE Fighter', 'Millenium Falcon']
const droids = ['R2-D2', 'C-3PO', 'K-2SO']

// define newDroids with droids reference
const newDroids = droids

// add 'BB-8' value to start of the new array created
newDroids.unshift('BB-8')

// the original variable is updated too, because it is the original reference
const droids = ['R2-D2', 'C-3PO', 'K-2SO']

// put new value in first element, define newDroids with a spread of droids, creating a new reference
const newDroids = ['BB-8', ...droids]

// the original variable wasn't updated
console.log(droids)
// ['R2-D2', 'C-3PO', 'K-2SO']
const darkSide = ['Darth Vader', 'Darth Sidious', 'Supreme Leader Snoke']

// define newDarkSide with darkSide reference
const newDarkSide = darkSide

// remove the last element of the new array created
newDarkSide.pop()

// the original variable is updated too, because it is the original reference
const darkSide = ['Darth Vader', 'Darth Sidious', 'Supreme Leader Snoke']

// remove the last element using slice method, creating a new reference
// in this example we don't need to use spread operator
const newDarkSide = darkSide.slice(0, darkSide.length - 1)

// the original variable wasn't updated
console.log(darkSide)
// ['Darth Vader', 'Darth Sidious', 'Supreme Leader Snoke']
const lightSide = ['Yoda', 'Luke Skywalker', 'Obi-Wan Kenobi']

// define newLightSide with lightSide reference
const newLightSide = lightSide

// remove the first element of the new array created
newLightSide.shift()

// the original variable is updated too, because it is the original reference
const lightSide = ['Yoda', 'Luke Skywalker', 'Obi-Wan Kenobi']

// remove the first element using slice method, creating a new reference
// in this example we don't need to use spread operator
const newLightSide = lightSide.slice(1)

// the original variable wasn't updated
console.log(lightSide)
// ['Yoda', 'Luke Skywalker', 'Obi-Wan Kenobi']