Skip to content

Instantly share code, notes, and snippets.

@Kishimoto96
Created March 28, 2023 18:54
Show Gist options
  • Save Kishimoto96/50d100eb026ea0b179d5e0b39f00ce20 to your computer and use it in GitHub Desktop.
Save Kishimoto96/50d100eb026ea0b179d5e0b39f00ce20 to your computer and use it in GitHub Desktop.

Destructuring & Spread Operator:

  1. What is destructuring in JavaScript and how does it work?
  2. How can the spread operator be used in JavaScript?
  3. What are some practical use cases for using destructuring and the spread operator in JavaScript?
  4. How does destructuring and the spread operator differ from traditional assignment and concatenation in JavaScript?
  5. Can destructuring and the spread operator be used with non-array and non-object data types in JavaScript?
@Sara-Nefise
Copy link

Team Members @muhammedhasann, Guled Khadar Abdi, Joud Khanji, Sara Nafisa

  1. Destructuring is a JavaScript expression that makes it possible to unpack values from arrays or properties from objects into distinct variables. For example, if you have an array x with values [1, 2, 3, 4, 5], you can use destructuring to assign the first two values of the array to variables y and z like this: const [y, z] = x;. After this assignment, y will be equal to 1 and z will be equal to 2.
    Similarly, you can destructure objects. For example, if you have an object obj with properties {a: 1, b: 2}, you can use destructuring to assign the values of properties a and b to variables a and b like this: const {a, b} = obj;. After this assignment, a will be equal to 1 and b
    will be equal to 2.

const numbers = [1, 2, 3];
const expandedNumbers = [...numbers, 4, 5];
console.log(expandedNumbers); // Output: [1, 2, 3, 4, 5]

spread opreator uses for shallow copy for an array or object
3. Destructuring use cases: Extracting values from arrays or object, Specifying default values,Swapping variables
spread operator use cases: Merging arrays and objects,Copying arrays and objects, Converting strings to arrays
5. yes, destructuring and spread Operator works with String and functions too




@irodamrj
Copy link

irodamrj commented Mar 30, 2023

1- Destructuring lets us pull out a specific parts of an array, object or a string. It would be easier to assign values to our variables.
2-spread operator allows us to do shallow copy on arrays, objects and strings. when we use spread operator to copy, and then make a modification to the copied array/objects/strings, it wouldnt affect the original ones.
3-when we want to get some of the attributes from an nested object, we can use destructuring. For spread operator: we can crate new array from the existing one.
4-In traditional assignment, we can create only one variable and assign it at a time. In destructuring, we can easily create and initialize more than one variable at a time. For concatenation: we can use spread operator with strings, objects and arrays.
5-NO

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment