Found 6710 Articles for Javascript

Which algorithm does the JavaScript Array#sort() function use?

Ayush Gupta
Updated on 16-Sep-2019 07:28:03

967 Views

Javascript specification doesn't specify a particular algorithm to be used in the Array.sort implementation. This is left on the implementor to decide. So different JS engines use different sorting algorithms.Mozilla(Spider Monkey JS engine) uses mergeSort. You can see the code written for it in C in the Mozilla repository: https://dxr.mozilla.org/seamonkey/source/js/src/jsarray.cWebKit(Chrome, Safari, etc) do not directly use a sorting algorithm, instead they choose the algorithm based on element types and length of the array. For example, Numeric arrays use C++ Std library's quick sort function.Non-numeric arrays use merge sort.In some other cases it uses selection sort.It depends on the datatype and ... Read More

How would you deep copy an Object in Javascript?

Manisha Patil
Updated on 23-Aug-2022 14:41:09

254 Views

Clone often refers to copying a value from one place to another. We duplicate one value to another using JavaScript, which is known as cloning. In JavaScript, there are actually two different sorts of copying. The ability to distinguish between deep and shallow clones is something that every programmer, regardless of experience level, should understand. Since this essay is about deep clones, we shall investigate deep clones in detail. Any data type, including composite data types like arrays and JavaScript, as well as primitive data types (like string and number), can experience the concept of cloning. Therefore, we must understand ... Read More

Passing unknown number of arguments to a function in Javascript

Manisha Patil
Updated on 23-Aug-2022 13:46:05

3K+ Views

The terms "parameters" and "arguments of a function" are frequently used synonymously in JavaScript, despite the fact that there is a substantial distinction between the two. The function parameters are included when we define a function. While defining a function, you may also specify a list of variables; these variables are referred to as function parameters. On the other hand, "function parameters" are the values we pass when we call or execute the newly created function. In JavaScript, the variables listed in the function declaration serve as the argument values. The arguments that are part of the function definition are ... Read More

Difference between push() and unshift() methods in javascript

Ayush Gupta
Updated on 16-Sep-2019 07:06:55

494 Views

The unshift method adds the element at the zeroeth index and shifts the values at consecutive indexes up, then returns the length of the array.The push() method adds the element at end to an array and returns that element. This method changes the length of the array.Examplelet fruits = ['apple', 'mango', 'orange', 'kiwi']; let fruits2 = ['apple', 'mango', 'orange', 'kiwi']; console.log(fruits.push("pinapple")) console.log(fruits2.unshift("pinapple")) console.log(fruits) console.log(fruits2)Output5 5 [ 'apple', 'mango', 'orange', 'kiwi', 'pinapple' ] [ 'pinapple', 'apple', 'mango', 'orange', 'kiwi' ]Note that both the original arrays were changed here.Unshift is slower than push because it also needs to unshift all the elements ... Read More

Difference between shift() and pop() methods in Javascript

Ayush Gupta
Updated on 16-Sep-2019 07:03:11

516 Views

The shift method removes the element at the zeroeth index and shifts the values at consecutive indexes down, then returns the removed value. If the length property is 0, undefined is returned.The pop() method removes the last element from an array and returns that element. This method changes the length of the array.Examplelet fruits = ['apple', 'mango', 'orange', 'kiwi']; let fruits2 = ['apple', 'mango', 'orange', 'kiwi']; console.log(fruits.pop()) console.log(fruits2.shift()) console.log(fruits) console.log(fruits2)Outputkiwi apple [ 'apple', 'mango', 'orange' ]  [ 'mango', 'orange', 'kiwi' ] Note that both the original arrays were changed here.Shift is slower than pop because it also needs to shift all ... Read More

What is JavaScript’s highest integer value that a Number can go to without losing precision?

Manisha Patil
Updated on 24-Aug-2022 10:08:44

22K+ Views

The value of the MAX SAFE INTEGER constant is 9007199254740991 (9, 007, 199, 254, 740, 991 or nine quadrillion). JavaScript only properly represents integers between -(253 - 1) and 253 - 1, which is the rationale for that number. JavaScript employs double-precision floating-point format numbers as specified in IEEE 754. Number and BigInt are the two number types available in JavaScript. Number, the most popular type of number, is a 64-bit floating point IEEE 754 number. Number.MAX SAFE INTEGER, which is the following, has the largest precise integral value of this type − 253-1, or +/- 9, 007, 199, ... Read More

Low level difference between Slice and Splice methods in Javascript

Ayush Gupta
Updated on 16-Sep-2019 06:56:26

424 Views

The basic difference between slice and splice is −splice() changes the original array on which it is called and returns the removed item(s) in an array as a new array object.slice() doesn't change the original array and also returns the array sliced.Example// splice changes the array let arr = [1, 2, 3, 4, 5]; console.log(array.splice(2)); //slice doesn't change original one let arr2 = [1, 2, 3, 4, 5]; console.log(array2.slice(2)); console.log(" After Changing the arrays"); console.log(array); console.log(array2);Output[ 3, 4, 5 ] [ 3, 4, 5 ]After Changing the arrays[[ 1, 2 ] [ 1, 2, 3, 4, 5 ]

Difference between JavaScript deepCopy and shallowCopy

Ayush Gupta
Updated on 16-Sep-2019 06:52:07

254 Views

Shallow copy and deep copy are language agnostic. Shallow copies duplicate as little as possible. A shallow copy of a collection is a copy of the collection structure, not the elements. With a shallow copy, two collections now share the individual elements.Examplelet innerObj = {    a: 'b',    c: 'd' } let obj = {    x: "test",    y: innerObj } // Create a shallow copy. let copyObj = Object.assign({}, obj); // Both copyObj and obj's prop y now refers to the same innerObj. Any changes to this will be reflected. innerObj.a = "test" console.log(obj) console.log(copyObj)Output{ x: 'test', ... Read More

Radix sort in Javascript?

Ayush Gupta
Updated on 16-Sep-2019 06:50:09

266 Views

The radix sort algorithm distributes integers into buckets based on a number's significant digit or value (the radix). The radix is based on the number system of the values of the arrays. Let us look at how it can be implemented −Examplefunction radixSort(arr) {    // Find the max number and multiply it by 10 to get a number    // with no. of digits of max + 1    const maxNum = Math.max(...arr) * 10;    let divisor = 10;    while (divisor < maxNum) {       // Create bucket arrays for each of 0-9     ... Read More

Merge sort vs quick sort in Javascript

Lokesh Yadav
Updated on 09-Dec-2022 10:17:35

684 Views

In this article, we are going to discuss about the differences between merge sort and quick sort in JavaScript with appropriate examples. Merge sort and Quick sort are used to sort the elements, but the approach is different. Both Merge sort and quick sort are based on Divide and Conquer Strategy. Merge sort It is a stable sorting algorithm. In the merge sort, it follows a recursive approach that repeatedly splits the array into half until no more division is possible i.e., the array either remains empty or has a single element. Then by comparing the two small array elements ... Read More

Advertisements