The JavaScript Array unshift() function is used to add one or more elements to the beginning of an array. It modifies the original array and returns the new length of the array. The elements are added in the order they appear in the function arguments. This function is useful when you want to add new elements to the beginning of an array without changing the order of the existing elements. Keep reading below to learn how to Javascript Array unshift in Kotlin.

Looking to get a head start on your next software interview? Pickup a copy of the best book to prepare: Cracking The Coding Interview!

Buy Now On Amazon

Javascript Array unshift in Kotlin With Example Code

JavaScript’s `unshift()` method is used to add one or more elements to the beginning of an array. In Kotlin, we can achieve the same functionality using the `plus()` operator.

To add an element to the beginning of an array in Kotlin, we can create a new array with the new element at the beginning, and then concatenate the original array to the end of the new array using the `plus()` operator. Here’s an example:


val originalArray = arrayOf("apple", "banana", "orange")
val newArray = arrayOf("pear") + originalArray
println(newArray.joinToString()) // Output: pear, apple, banana, orange

In this example, we create a new array `newArray` with the element “pear” at the beginning, and then concatenate the original array `originalArray` to the end of `newArray` using the `plus()` operator. Finally, we print the contents of `newArray` using the `joinToString()` method.

We can also add multiple elements to the beginning of an array by creating a new array with the new elements at the beginning, and then concatenating the original array to the end of the new array using the `plus()` operator. Here’s an example:


val originalArray = arrayOf("apple", "banana", "orange")
val newElements = arrayOf("pear", "grape")
val newArray = newElements + originalArray
println(newArray.joinToString()) // Output: pear, grape, apple, banana, orange

In this example, we create a new array `newArray` with the elements “pear” and “grape” at the beginning, and then concatenate the original array `originalArray` to the end of `newArray` using the `plus()` operator. Finally, we print the contents of `newArray` using the `joinToString()` method.

In summary, to add one or more elements to the beginning of an array in Kotlin, we can create a new array with the new element(s) at the beginning, and then concatenate the original array to the end of the new array using the `plus()` operator.

Equivalent of Javascript Array unshift in Kotlin

In conclusion, the Kotlin programming language provides a powerful and efficient way to manipulate arrays using the equivalent of the JavaScript Array unshift function. The Kotlin MutableList class offers the add function, which can be used to add elements to the beginning of an array. This function works similarly to the unshift function in JavaScript, allowing developers to easily modify arrays in Kotlin. With this functionality, Kotlin developers can easily manipulate arrays and create dynamic and responsive applications. Overall, the Kotlin programming language provides a robust set of tools for array manipulation, making it a great choice for developers looking to build high-performance applications.

Contact Us