Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

We often use anonymous functions as arguments of other functions. For example:

setTimeout(function () {
    console.log('Execute later after 1 second')
}, 1000);Code language: JavaScript (javascript)
Comment

anonymous functions javascript

/*
Anonymous function = a function that doesn't need a name, you only need it for the 
purpose of a single callback
*/

// e.g.

const arr = [1,2,3,4]
arr.map(function square(num){
	return num * 2
})

// the 'square' function is only used within .map here. So it can be treated as an 
// anonymous function. Short hand for an anonymous function can look like this:

arr.map(function(num){
	return num * 2
})

// Or using arrow notation it can look like this:

arr.map((num) => {
	return num * 2
})
Comment

anonymous functions in javascript

// Create calculator calling different operators with functions in javascript 

function pow(value0, value1) {
    return Math.pow(value0, value1);
}

function mod(value0, value1) {
    return value0 % value1;
}

function div(value0, value1) {
    return value0 / value1;
}

function mul(value0, value1) {
    return value0 * value1;
}

function add(value0, value1) {
    return value0 + value1;
}

function subtract(value0, value1) {
    return value0 - value1;
}

function calculator(value0, value1, operator) {
    return operator(value0, value1);
}

console.log(calculator(2,5,mod));
Comment

Anonymous Functions with arguments in JavaScript

let printName = function (name) {  
    console.log('Hello ',name);  
};  

printName('Chandler Bing');
Comment

anonymous function parameters javascript

function caller(otherFunction) {
     otherFunction(2);
 }
caller(function(x) {
    console.log(x); 
});
Comment

Anonymous Functions in JavaScript

let text = function () {  
    console.log('Hello World');  
};  

text();
Comment

Using anonymous functions as arguments of other functions

setTimeout(function () {
    console.log('Welcome to JavaScript world')
}, 1000);
Comment

PREVIOUS NEXT
Code Example
Javascript :: send json by curl 
Javascript :: liquid filter 
Javascript :: split javascript 
Javascript :: js set to array 
Javascript :: How to make a toggle button in Angularjs 
Javascript :: login condition if and else in router dom of react jsx 
Javascript :: call javascript function from python 
Javascript :: sort array 
Javascript :: push an item to array javascript 
Javascript :: react native charts 
Javascript :: ng2-tel-input phone number code 
Javascript :: for loop in javacript 
Javascript :: js concate map 
Javascript :: remove element json javascript 
Javascript :: mongodb rename property 
Javascript :: firebase realtime database javascript 
Javascript :: join javascript array 
Javascript :: socket io websocket connection 
Javascript :: javascript static 
Javascript :: bot react message with custom emoji 
Javascript :: static in javascript 
Javascript :: regex serch in capture group 
Javascript :: unit testing for react 
Javascript :: JavaScript Debug usage Example 
Javascript :: what is palindrome 
Javascript :: Requiring express 
Javascript :: pass component as props react 
Javascript :: best computer language 
Javascript :: jquery check if click to this self not this child 
Javascript :: filter based on input typing react 
ADD CONTENT
Topic
Content
Source link
Name
8+7 =