Found 6710 Articles for Javascript

Method Chaining in JavaScript

Arnab Chakraborty
Updated on 23-Aug-2022 06:34:35

8K+ Views

Method or function chaining is a popular method in JavaScript that is used to write more concise and readable code. In this article, we shall discuss the method of chaining tactics in JavaScript and also discuss how it works In some JavaScript programs written with JQuery or some other packages, sometimes we call multiple functions one after another on the same line. This can be explained by a simple example as shown below − Syntax ob = ob.method_1().method_2().(some more methods).method_n() Example Without using the method of chaining. HTML Console ... Read More

Example of a circular reference in Javascript

Arnab Chakraborty
Updated on 05-Apr-2023 12:17:28

2K+ Views

Circular referencing is an idea when an object directly or indirectly refers to itself back. It creates a closed loop. Like other programming languages, javascript also faces this problem of circular referencing. In this article, we shall cover a few examples of different circular referencing modes in javascript. Direct Circular Referencing In direct circular referencing the object is pointing itself by using the self pointer. In this example, we can expand the user object infinitely (in practice up to a certain limit) and all are pointing to the same object. Let us see the following code and the output console ... Read More

Eradicating Memory Leaks in Javascript

Ayush Gupta
Updated on 17-Sep-2019 08:15:58

182 Views

The main cause for leaks in garbage collected languages are unwanted references. To understand memory leaks, let us see how the memory freeing(garbage collection) works.Mark-and-sweep algorithm −This algorithm reduces the definition of "an object is no longer needed" to "an object is unreachable". This algorithm assumes the knowledge of a set of objects called roots. In JavaScript, the root is the global object. Periodically, the GC starts from these roots, find all objects that are referenced from these roots, recursively. Starting from the roots, the GC will thus find all reachable objects and collect all non-reachable objects.Types of memory leaks1. ... Read More

Practical Uses for Closures in Javascript?

Arnab Chakraborty
Updated on 22-Aug-2022 13:55:39

724 Views

In JavaScript, the closure is an environment which helps us to access an outer function’s scope from an inner function. In JavaScript, when a function is created, the closure also created. In other words, we can say closure is a way which allows functions that is present inside outer functions to access the outer function’s scope. In this article we are focusing on some practical uses of Closures in JavaScript. Syntax function outerFunction(){ function updateProperty(){ } return updateProperty() } ... Read More

How to avoid circular reference in OOP Javascript?

Ayush Gupta
Updated on 17-Sep-2019 08:05:54

491 Views

A circular reference occurs if two separate objects pass references to each other.In older browsers circular references were a cause of memory leaks. With improvements in Garbage collection algorithms, which can now handle cycles and cyclic dependencies fine, this is no longer an issue.However, from a pure design point of view, circular referencing is still a bad thing and a code smell. Circular referencing implies that the 2 objects referencing each other are tightly coupled and changes to one object may need changes in other as well.Avoiding circular referencesThere is no one way to avoid circular reference in JS. It ... Read More

Why circular reference is bad in javascript?

Ayush Gupta
Updated on 17-Sep-2019 08:02:47

1K+ Views

A circular reference occurs if two separate objects pass references to each other.In older browsers circular references were a cause of memory leaks. With improvements in Garbage collection algorithms, which can now handle cycles and cyclic dependencies fine, this is no longer an issue.However, from a pure design point of view, circular referencing is still a bad thing and a code smell. Circular referencing implies that the 2 objects referencing each other are tightly coupled and changes to one object may need changes in other as well.Avoiding circular referencesThere is no one way to avoid circular reference in JS. It ... Read More

Garbage collection(GC) in JavaScript?

Arnab Chakraborty
Updated on 22-Aug-2022 13:39:31

2K+ Views

Memory management in JavaScript is much easier than in low-level languages like C or C++. Unlike low-level languages, JavaScript automatically detects which objects will be needed in later cases and which will be garbage that is occupying memory without any reason. In this article, we shall discuss how garbage collection (GC) works in JavaScript. There are a few important parts that we are going to discuss in detail− Reachability from root Interlinked Objects (interconnections between different objects) Unreachable Collections (Broken link) Reachability The first concept of memory management is reachability. In this mode, the garbage collector tries to ... Read More

Difference between == and === operator in JavaScript

Kiran Kumar Panigrahi
Updated on 23-Jun-2023 13:41:03

9K+ Views

JavaScript is widely used to create interactive web pages. It has many frameworks such as React JS, Angular JS, Node JS, etc. Like any other programming language, JavaScript also provides operators like arithmetic, relational, comparison operators, etc. The equality operator, i.e., "==" is one such comparison operator that checks whether the LHS is equal to RHS or not. This operator is present in all other programming languages but it is somewhat different in JavaScript. This is due to the fact that JavaScript is a loosely typed language, whereas all other languages are strictly typed. As JS is loosely typed, it ... Read More

Memory Management in JavaScript

Arnab Chakraborty
Updated on 22-Aug-2022 12:59:07

1K+ Views

Memory management is an essential task when writing a good and effective program in some programming languages. This article will help you to understand different concepts of memory management in JavaScript. In low-level languages like C and C++, programmers should care about the usage of memory in some manual fashion. On the other hand, Javascript automatically allocates memory when objects are created into the environment and also it cleans the memory when an object is destroyed. JavaScript can manage all of these on its own but this does not imply that the developers do not need to worry about the ... Read More

What is the use of .clear() method in Javascript weakMap?

Abdul Rawoof
Updated on 26-Aug-2022 11:58:18

561 Views

WeakMap is a collection in JavaScript. This type of collection is used to store the data in the form of key-value pairs. In WeakMap, the key must be definitely an object and the values can be of any type. This collection is called as WeakMap because of the key which is mandated to be the object. An object can be garbage collected which is a disadvantage when compared with Map. In JavaScript WeakMap, clear() function is used to delete the whole weakMap or removes all the elements in the weakMap. Syntax the syntax for weak map is as follows. mapName.clear() ... Read More

Advertisements