Found 9054 Articles for Front End Technology

What are the best practices for function overloading in JavaScript?

Nitya Raut
Updated on 16-Jun-2020 11:42:02

164 Views

Function overloading occurs when a function performs different tasks based on a number of arguments passed to it.The best practice for function overloading with parameters is to not check the types. The code runs slower when the type is checked and it should be avoided. For this, the last parameter to the methods should be an objectAlso, do not check the argument length.ExampleHere’s an example −function display(a, b, value) { } display(30, 15, {"method":"subtract"}); display(70, 90, {"test":"equals", "val":"cost"});

How do I view events fired on an element in Chrome?

Fendadis John
Updated on 30-Jul-2019 22:30:22

12K+ Views

To view events fired on an element, follow the below steps in Google Chrome:Open Google Chrome and press F12 to open Dev Tools.Now go to Sources TabGo to Event Listener Breakpoints, on the right:Click on the events and interact with the target element.If the event will fire, then you will get a breakpoint in the debugger.

What is the usage of yield keyword in JavaScript?

usharani
Updated on 03-Oct-2019 07:08:09

127 Views

The yield keyword is used in JavaScript to pause and resume a generator function. The value of the expression is returned to the generator's caller.Here’s the syntax, where “exp” is the expression and the optional value is returned by “val”, which is passed to the generator's next() method.[val] = yield [exp];Here are the examples:function* displayRank () {    var selPlayers= [1, 2, 3, 4];    for (var a = 0; a < selPlayers.length; a++) {       yield selPlayers[i];    } }After defining a generator function, use it like the following.Here displayRank() is the generator function:var rank = displayRank(); ... Read More

What are immediate functions in JavaScript?

varun
Updated on 16-Jun-2020 06:18:43

242 Views

The immediate function executes as soon as it is defined. To understand the role of immediate function, let’s see the difference between a function and an immediate function −Here’s immediate function −(function() {    var str = "display"; }()); function display() {    // this returns undefined    alert(str); }Here’s a function −var str = "display"; function display() {    // This returns "display"    alert(str); }Let’s see another example of immediate functions −var name = 'Amit'; (function(sName) {    alert( 'Student name = ' + sName ); }(sName))

How to preserve variables in a JavaScript closure function?

Shubham Vora
Updated on 20-Oct-2022 07:34:57

921 Views

In this tutorial, we will look at the method to preserve variables in a JavaScript closure function. What is a closure function? A closure function gives access to the scope of an outer function from an inner function. It also allows private variables. Closure variables are stored in stack and heap. When a function is created, closure is also created. Closure remembers external things used in it. Closures are the primary mechanism for data privacy. One drawback is that the variables used are not garbage collected. Overuse of closure functions will damage the system due to redundant ... Read More

Why are parenthesis used to wrap a JavaScript function call?

Rishi Rathor
Updated on 16-Jun-2020 11:12:24

4K+ Views

In JavaScript, the functions wrapped with parenthesis are called “Immediately Invoked Function Expressions" or "Self Executing Functions.The purpose of wrapping is to namespace and control the visibility of member functions. It wraps code inside a function scope and decrease clashing with other libraries. This is what we call Immediately Invoked Function Expression (IIFE) or Self Executing Anonymous Function.Here’s the syntax −(function() {    // code })();As you can see above, the following pair of parentheses converts the code inside the parentheses into an expression −function(){...}In addition, the next pair, i.e. the second pair of parentheses continues the operation. It calls ... Read More

How to set Text alignment in HTML?

Sharon Christine
Updated on 29-Aug-2023 07:12:50

216K+ Views

To set text alignment in HTML, use the style attribute. The style attribute specifies an inline style for an element. The attribute is used with the HTML tag, with the CSS property text-align for the center, left, and right alignment. HTML5 do not support the align attribute of the tag, so the CSS style is used to set text alignment.Just keep in mind, the usage of style attribute overrides any style set globally. It will override any style set in the HTML tag or external style sheet.ExampleYou can try to run the following code to set text ... Read More

What is the difference between closure and nested functions in JavaScript?

mkotla
Updated on 09-Jan-2020 06:52:53

764 Views

JavaScript ClosuresIn JavaScript, all functions work like closures. A closure is a function, which uses the scope in which it was declared when invoked. It is not the scope in which it was invoked.Here’s an exampleLive Demo           JavaScriptClosures                       varp = 20;             functiona(){                var p = 40;                b(function(){                   alert(p);             ... Read More

How to set font color in HTML?

Lokesh Badavath
Updated on 29-Aug-2023 07:35:05

147K+ Views

We use the style attribute to set the font color in HTML. The style attribute specifies an inline style for an element, with the CSS color property. The attribute is used with the HTML tag, with the CSS color property. HTML5 do not support the tag, so the CSS style is used to add font color. The tag deprecated in HTML5. Syntax text… Example In the example below, we set the font color of the text inside the tag. DOCTYPE html> HTML Font color ... Read More

How does recursion function work in JavaScript?

Shubham Vora
Updated on 22-Jul-2022 13:16:55

467 Views

This article will teach you how to create a JavaScript recursive function-a function that calls itself-using the recursion method. The process of recursion involves calling itself. Recursive functions are those that call themselves repeatedly. A condition to cease calling itself is always included in recursive functions. Otherwise, it will continue to call itself. Recursive functions are typically used to divide a large issue into smaller ones. Recursive functions are frequently found in data structures like binary trees, graphs, and algorithms like binary search and quick-sort.A recursive function is not immediately clear or simple to comprehend. You will read and comprehend ... Read More

Advertisements