Javascript Articles - Page 396 of 534

How can Detached DOM elements cause memory leak in JavaScript?

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

2K+ Views

Detached Dom elementsDetached DOM elements are the elements which have been removed from the DOM but their memory is still retained because of JavaScript. This means that as long the element have a reference to any variable or an object anywhere, it does not garbage collected even after destroyed from the DOM.DOM is like an double-linked tree which means a reference to a node in the tree will halt the entire tree from garbage collection.Let's take an example of creating a DOM element in javascript. After creating the element destroy it but forget to delete the variable holding it. This ... Read More

How to clone an object in JavaScript?

Nikhilesh Aleti
Updated on 18-Nov-2022 07:19:09

5K+ Views

An object is said to be an entity which has properties and types in it. Example, consider a Person as an object and it has properties like height, weight, age and salary. In the similar way JavaScript also have objects and their defined properties. An object in JavaScript is a complicated data type where it can store various data types in it. Let’s consider this example below const employee = { name : ‘Dhoni’, age : 41, height : 5.8, } Cloning an object using JavaScript In general, “Cloning” is ... Read More

Explain in detail about Mark and Sweep algorithm in JavaScript?

vineeth.mariserla
Updated on 04-Jul-2020 15:01:53

1K+ Views

Mark and Sweep algorithmMark and Sweep algorithm looks out for objects 'which are unreachable' rather than objects 'which are no longer needed'. This algorithm is the improvement of Reference-counting algorithm.This algorithm actually goes through 3 important steps. Root: In general, a root is a global variable that is used in the code. A window object in javascript can act as a   root. This algorithm uses global object root to find whether the objects are reachable or unreachable.This algorithm then monitors every root and also their children. While monitoring, some objects which are reachable are marked and remaining objects which are ... Read More

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

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

457 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 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

How to insert an 8-byte integer into MongoDB through JavaScript shell?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

261 Views

You can use below syntax for inserting an 8-byte integer in MongoDB through JavaScript shell −anyVariableName= {"yourFieldName" : new NumberLong("yourValue")};To display the above variable, you can use the variable name or printjson(). Following is the query to insert 8 byte integer in MongoDB −> userDetail = {"userId" : new NumberLong("98686869")}; { "userId" : NumberLong(98686869) }Let us display the above variable value using variable name. The query is as follows −> userDetailThis will produce the following output −{ "userId" : NumberLong(98686869) }Let us display the variable value using printjson() −> printjson(userDetail); This will produce the following output −{ "userId" : NumberLong(98686869) }

How to find the minimum value of an array in JavaScript?

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

3K+ Views

We can find the minimum value of an array using Math.min() and sort()1) Math.min() Math.min() function usually takes all the elements in an array and scrutinize each and every value to get the minimum value.It's methodology is discussed in the below example. Live DemoExample    function minimum(value) {       if (toString.call(value) !== "[object Array]")       return false;       return Math.min.apply(null, value);    }    document.write(minimum([6, 39, 55, 1, 44])); Output12)  sort()Using sort() and compare functions we can write the elements in ascending or descending order, later on using length property we can ... Read More

How to execute a cube of numbers of a given array in JavaScript?

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

2K+ Views

To find the cubes of elements of given array we need to run a for loop to access each and every element and we need to use "=" operator to replace elements with their cubes.To get the desired values the below steps need to be followed.Steps1) Declared an array a = [1, 2, 3, 4, 5]2) For loop was introduced to access each and every element in the array(a[i]).3) Inside for loop "=" operator is used to replace the element with their cubic values(a[i]*a[i]*a[i]).so, from the above steps the output obtained will be 1, 8, 27, 64, 125.By following the ... Read More

Advertisements