Found 6685 Articles for Javascript

How to execute a JavaScript function when I have its name as a string?

Shubham Vora
Updated on 23-Aug-2022 09:18:14

2K+ Views

Calling a function from a string, stored in a variable can be done in two different ways. The first approach makes use of the window object method, and the second method makes use of the eval() function. This tutorial will guide you to learn the way to execute a JavaScript function using its name as a string. Using the window[]() Method Here, to execute the function using the function name as a string, the string should be changed to a pointer using the following syntax. Syntax var functionName = "string"; functionName = window[functionName]( parameters ); Here we have stored ... Read More

What is the use of JavaScript eval function?

Vrundesha Joshi
Updated on 12-Jun-2020 09:14:50

262 Views

The JavaScript eval() is used to execute an argument. The code gets execute slower when the eval() method is used. It also has security implementations since it has a different scope of execution. In addition, use it to evaluate a string as a JavaScript expression.The eval() method is not suggested to use in JavaScript since it executes slower and improper usage opens your website to injection attacks.ExampleHere’s how you can implement eval() functionLive Demo                    var a = 30;          var b = 12;          var res1 = eval("a * b") + "";          var res2 = eval("5 + 10") + "";          document.write(res1);          document.write(res2);          

What is the (function() { } )() construct in JavaScript?

Rishi Rathor
Updated on 16-Jun-2020 09:15:21

1K+ Views

The (function() { } )() construct is an immediately invoked function expression (IIFE). It is a function, which executes on creation.SyntaxHere’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 the function, which resulted from the expression above.

How to check if a JavaScript function is defined?

Shubham Vora
Updated on 08-Aug-2022 08:40:53

13K+ Views

In this tutorial, we will learn to check whether the JavaScript function is defined or not. If a programmer calls the JavaScript function without defining it, they will see the reference error with the message like “function is not defined.” To overcome the problem, the programmer can check whether the function is defined and call the function. We will look at the various approaches to check if the function is defined or not in JavaScript below. Using the typeof Operator In JavaScript, the typeof operator is useful to check the type of the variable, function, objects, etc. When we use ... Read More

What is "function*" in JavaScript?

Smita Kapse
Updated on 12-Jun-2020 09:02:44

190 Views

The function* declaration is used to define a generator function. It returns a Generator object. Generator Functions allows execution of code in between when a function is exited and resumed later. So, generators can be used to manage flow control in a code.SyntaxHere’s the syntax −function *myFunction() {} // or function* myFunction() {} // or function*myFunction() {}Let’s see how to use generator functionExampleLive Demo                    function* display() {             var num = 1;             while (num < 5)       ... Read More

How to find JavaScript function definition in Chrome?

Abhishek
Updated on 31-Oct-2022 07:26:46

6K+ Views

In today’s time, almost everyone knows Google Chrome and how to use it for their comfort. But in the case of a developer, it is a must to have advanced knowledge of using Chrome because Chrome can help a developer tackle problems in many ways such as solving the problems or debugging the code with ease, inspecting the code, and finding JavaScript functions defined in the code. In this tutorial, we will learn how we can find a JavaScript function in Google Chrome that is defined in the JavaScript code. Here are three ways of finding the JavaScript function in ... Read More

What is the purpose of wrapping whole JavaScript files in anonymous functions?

Anvi Jain
Updated on 12-Jun-2020 08:58:55

745 Views

The purpose of wrapping is to a namespace and control the visibility of member functions. It wraps the code inside a function scope and decreases clashing with other libraries. This is what we call Immediately Invoked Function Expression (IIFE) or Self Executing Anonymous Function.SyntaxHere’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 the function, which resulted from the expression above.

How do you find out the caller function in JavaScript?

Shubham Vora
Updated on 25-Jul-2022 08:35:18

6K+ Views

In this tutorial, we will learn to find out the caller function in JavaScript. The function is the reusable code, and users can call it from anywhere. But sometimes, they need to know who is the caller function to perform some operation.For example, suppose that we can call any single function from another 2 to 3 functions, and we need to perform some operation according to the caller function. Users can understand this scenario by the below code example.function func3 () {    If ( caller is func2() ) {       // code for some operation    } else if ( caller is func1() ){       // code for some different operation    } ... Read More

How do I declare a namespace in JavaScript?

Shubham Vora
Updated on 22-Jul-2022 12:22:55

3K+ Views

A namespace is a programming concept that gives identifiers (names of types, functions, variables, etc.) scope to avoid name conflicts. For instance, a program may need to use the same variable name in many contexts. In such a case, namespaces will separate these contexts so that the same identifier may be utilized in many namespaces.The initialization and application of namespaces in JavaScript will be covered in this tutorial. A namespace is not by default available in JavaScript. Constructing a global object that contains all functions and variables, the JavaScript Namespace feature may be repeated. Because numerous libraries and components are ... Read More

What does the exclamation mark do before the function in JavaScript?

Rishi Rathor
Updated on 12-Jun-2020 08:46:17

2K+ Views

The ! symbol shows that it is an immediately-invoked function expression.The exclamation mark won’t invoke the function alone; you can put () at the end −!function foo() {}()() has higher precedence than ! and instantly calls the function.You can also mention it like the following −(function(){})();The ! allows the expression to return true. This is because by default all immediately-invoked function expression return undefined, so, we’re left with ! undefined, which is true.

Advertisements