DEV Community

Cover image for Spread operator in JavaScript VS Python.
Aditya
Aditya

Posted on • Edited on

5 1 1 1 1

Spread operator in JavaScript VS Python.

The JavaScript spread operator(...) becoming popular in programming as it is used for expanding iterable objects into function arguments, array literals, or other object literals.

Simply saying, spread operator can be used as an unpacking tools which expands the iterables into individual elements

On the other hand, Python too contains an alternative for Spread operator that allows the iterables unpacking.

Now we will see it in some of the examples shown-

Array Literals

In JS

const name = ['Aditya', 'Shivam', 'Yash'];
 const newname = ['Sahil', ...name, 'Niraj']; //spreading name in newname

 console.log(newname);

//Output:['Sahil', 'Aditya', 'Shivam', 'Yash', 'Niraj']
Enter fullscreen mode Exit fullscreen mode

Similarly,
In Python

name = ['Aditya', 'Shivam', 'Yash']
newname = ['Sahil', *name, 'Niraj']# unpacking name in newname

print(newname)

#Output: ['Sahil', 'Aditya', 'Shivam', 'Yash', 'Niraj']
Enter fullscreen mode Exit fullscreen mode

As we can see, we achieved the same result in Python as we got using the spread operator in JS.

Function Arguments

In JS

function add(a,b){
    return a+b;
}
const nums = [20,30];

console.log(add(...nums));//spreading nums while passing argument to a function

//Output: 50
Enter fullscreen mode Exit fullscreen mode

Similarly,
In Python

def add(a,b):
    return a+b
nums = [20,30]
print(add(*nums)#unpacking nums list while passing it to the add method
Enter fullscreen mode Exit fullscreen mode

Here we used the approach to get the similar output as we got in JS using spread operator.

Object Literals

In JS

const person = {
    name : 'Aditya',
    age : '23',
    occupation : 'Developer',
    height:'170 cm'
}

console.log({...person,location : 'India'})

//Output: {name: 'Aditya', age: '23', occupation: 'Developer', height: '170 cm', location: 'India'}
Enter fullscreen mode Exit fullscreen mode

Similarly, in Python we can use the double asterisk operator (**)

In Python

person = {
    'name' : 'Aditya',
    'age' : '23',
    'occupation' : 'Developer',
    'height' : '170 cm'
}

print({**person, 'location':'India'})

//Output: {'name': 'Aditya', 'age': '23', 'occupation': 'Developer', 'height': '170 cm', 'location': 'India'}
Enter fullscreen mode Exit fullscreen mode

As we can see, we needed **person to unpack the keyword arguments. Whereas, single asterisk operator *nums is used for iterable objects.

For more, follow me on Github, LinkedIn

Image of Stellar post

🚀 Stellar Dev Diaries Series: Episode 1 is LIVE!

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

Top comments (0)

Image of PulumiUP 2025

Let's talk about the current state of cloud and IaC, platform engineering, and security.

Dive into the stories and experiences of innovators and experts, from Startup Founders to Industry Leaders at PulumiUP 2025.

Register Now

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay