Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why javascript splice called splice even though it doesn't join but splits the array

JavaScript splice method actually splits an array into two as shown in the below example:

var a = ['a','b','c'];
var b = a.splice(0,2);
console.log(a); // outputs ['c']
console.log(b); // outputs ['a'.'b']

Then why is it called splice since splice means to join

like image 584
iJade Avatar asked Aug 24 '26 05:08

iJade


2 Answers

I'd imagine its named after RNA/DNA splicing where both addition and removal are called splicing: http://en.wikipedia.org/wiki/RNA_splicing

The splice method in JavaScript can both add and remove, so yeah.

like image 59
syazdani Avatar answered Aug 25 '26 18:08

syazdani


Actually JavaScript: the first 20 years said this

1.2 [Netscape 1997c]. Most of the library additions were inspired by features available in other popular languages. The Array concat and slice methods were inspired by Python’s sequence op- erations. The Array push, pop, shift, unshift, and splice were directly modeled on like-named Perl array functions.

New Feature In JavaScript 1.2

So it came from perl

like image 38
Qiulang 邱朗 Avatar answered Aug 25 '26 19:08

Qiulang 邱朗



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!