본문 바로가기
LEARN/JAVASCRIPT

[JavaScript] Spread operator

by 아이엠제니 2023. 1. 17.

 

 

I can use spread operator when I use object.

We use same object-key sometimes.

This operator can reduce repeat.

 

For example, First, I made 'cookie' object.

        const cookie = {
            base: "cookie",
            madeIn: "korea"
        }

 

 

and second, I made another object.

        const cookie = {
            base: "cookie",
            madeIn: "korea"
        }

        const chocochipCookie = {
            base: "cookie",
            madeIn: "korea",
            topping: "chocochip"
        }

You know.

Key 'base' and 'madeIn' are same.

We need simple code.

Spread operator can reduce this code.

 

 

        const cookie = {
            base: "cookie",
            madeIn: "korea"
        }

        const chocochipCookie = {
            ...cookie,
            topping: "chocochip"
        }

        const blueberryCookie = {
            ...cookie,
            topping: "blueberry"
        }

        const strawberryCookie = {
            ...cookie,
            topping: "strawberry"
        }

        console.log(cookie)
        console.log(chocochipCookie)
        console.log(blueberryCookie)
        console.log(strawberryCookie)

Finally, I reduced this code.

I use the spread operator for repeated space.

 

I think this operator is really simple!

I just need 'period'.

 

 

 

My English skills and grammar are not good.
But I'm trying!
I want to improve my English skills.
So sometimes I will write in English .
Thank you for visiting.

I'm junior developer!
I'm learning! 😊
300x250