Found 6710 Articles for Javascript

How to implement merge sort in JavaScript?

Nikhilesh Aleti
Updated on 23-Sep-2022 10:53:24

9K+ Views

Merge Sort The Merge Sort algorithm, where we can sort the elements in a particular order. This algorithm is also considered as an example of divide and conquer strategy. In merge sort algorithm, firstly the array will be divided into two parts and combined a particular sorted manner. The array will be divided into half until it cannot be divided. This means that if the array is completely divided and cannot be further divided, the dividing will be stopped. We divide the arrays into halves and implement merge sort on each of the halves. This algorithm is a ... Read More

How can circular references cause memory leakage in JavaScript?

vineeth.mariserla
Updated on 30-Jul-2019 22:30:26

541 Views

Circular reference A circular reference is formed when two variables reference to each other there by giving each object a reference count of 1.In pure garbage collected system, a circular reference may not be a problem when the variables involved were have no references.In that scenario the declared variables will be garbage collected.In reference counting system neither of the objects will be destroyed, because the reference count cannot be zero.In hybrid system, where reference counting and garbage collection are used, memory leaks will occur because the system fails to identify circular references.ExampleThe following example shows a circular reference between javascript object ... Read More

Explain in detail about memory leaks in JavaScript?

vineeth.mariserla
Updated on 04-Jul-2020 14:46:34

580 Views

Memory leaks in JavaScriptJavaScript is called garbage collected language,  that is when variables are declared, it will automatically allocate memory to them. When there are no more references for the declared variables, allocated memory will be released. Memory leaks or most of the memory related problems will occur while releasing the memory. Some common JavaScript leaks 1) Accidental global variablesWhen a undeclared variable is referenced, javascript creates a new variable in the global object. In the following Example-1 let's say the purpose of languages is to only reference a variable in the "myArray" function. If we don't use var to declare it ... Read More

Explain in detail about Reference-counting garbage collection in JavaScript?

vineeth.mariserla
Updated on 30-Jul-2019 22:30:26

3K+ Views

 Reference-counting garbage collectionThis is the simplest garbage collection algorithm.This algorithm looks out for those objects which have no references   left.An object becomes eligible for garbage collection if it has no references attached to it.The garbage collection is   explained in the below example.Examplevar obj = {       x: { y: 2 }          };          // 2 objects created. One is referenced by the other as one of its properties.          // Obviously, none can be garbage-collected obj = 1; // what was the 'x' property of the ... Read More

Explain in detail about the memory life cycle of JavaScript?

vineeth.mariserla
Updated on 30-Jul-2019 22:30:26

404 Views

Memory cycleRegardless of the programming language, the memory cycle is almost same for any programming language. There are 3 steps in a memory life cycle1) Allocation of memory .2) use the allocated memory(reading or writing)3) Release the allocated memory when it is unnecessary.The first and last parts are directly connected in low-level languages but are indirectly connected in high-level languages such as JavaScript.1) Allocation of memory in javascriptJavaScript is called garbage collected language, that is when variables are declared, it will automatically allocate memory to them.When there are no more references for the declared variables, allocated memory will be released. ExampleIn the ... Read More

What is the use of Array.entries() method in JavaScript?

vineeth.mariserla
Updated on 30-Jul-2019 22:30:26

212 Views

Array.entries()The Array.entries() in JavaScript is used to get a new Array that contains the key and value pairs for each index of an array. It returns an Array Iterator object with key/value pairs.syntaxarray.entries();ExampleIn the following example elements.entries() method, using for loop, access every element and generate key/value pair of the particular element.To simplify the code we just assigned an iterator variable to elements.entries()..Live Demo    var elements = ["Helium", "Neon", "Krypton", "Xenon", "Radon"];    var iterator = elements.entries();    for (let e of iterator) {       document.write(e);       document.write("");    } Output0, ... Read More

What is the difference between Math.ceil() and Math.round() methods in JavaScript?

vineeth.mariserla
Updated on 30-Jul-2019 22:30:26

6K+ Views

Math.ceil() and Math.round() methods differ in a way that the former round off a number to its nearest integer in upward direction of rounding(towards the greater value) whereas the latter round off a number to its nearest integer in downward direction of rounding(towards lower value). Let's examine the two methods individually.Math.ceil()Math.ceil() method round off number passed as parameter to its nearest integer so as to get greater value.ExampleIn the below example when a number 5.34 passed as a parameter, Math.ceil() round off it in to 6, which is a greater value than actual number.Live Demo document.write(Math.ceil(5.34)); ... Read More

what is the use of slice() method in JavaScript?

vineeth.mariserla
Updated on 30-Jul-2019 22:30:26

276 Views

slice()The slice() method extracts a part of a string and returns the extracted part in a new string.It doesn't modify the original string.syntaxslice() takes two parameters one is starting index and the other is ending index. It's notation is given below. string.slice(string.slice(starting index, ending index))Arguments   a) starting index: It gives from which index string extraction should be started.   b) ending index: It gives before which index string extraction should be ended.Example-1In the following example, slice() method slice the given string in to new string starting from index 18 to 26 (27-1) there by giving "Neuralink" as the output. Live Demo ... Read More

What is the use of substr() method in JavaScript?

vineeth.mariserla
Updated on 30-Jul-2019 22:30:26

215 Views

substr()substr() method extracts parts of a string, beginning at the character at the specified index, and returns the specified number of characters. It does not change the original string. Syntaxsubstr() method accepts two parameters one is start and other is length str.substr(start , length)Arguments   a) Start: start defines the starting index from where the sub string is to be extracted from the base string.   b) length: length defines the number of characters to be extracted starting from the start in the given string. If the                          second argument to the function is ... Read More

What is the use of Array.findIndex() method in JavaScript?

Abdul Rawoof
Updated on 25-Aug-2022 12:26:49

466 Views

Array is a data type which can store multiple elements of similar data types. For example, if array is declared as integer data type then it stores one or more elements of the integer data type. In JavaScript, Arrays are objects and these objects have some inbuilt functions and properties by which one can do the operations faster and easier. In this tutorial a functionality of array object ‘findIndex()’ is explained and demonstrated with some examples. The ‘findIndex()’ method in JavaScript arrays will return the first element from the given array with the given constraint being satisfied. This method will ... Read More

Advertisements