how double parantheses ()() workes in JavaScript? [duplicate] - javascript

This question already has answers here:
Two sets of parentheses after function call
(4 answers)
Closed 4 years ago.
Here i have a question regarding Double parentheses, can anyone please explain me how does it works.
function counter() {
console.log("hI");
return function (){
console.log("inside hI");
}
}
counter()();

In your example counter returns a function, so having parentheses after the call to counter calls the function that counter returns

Related

How can I use the setTimeout() statement? [duplicate]

This question already has answers here:
Why is the method executed immediately when I use setTimeout?
(8 answers)
How can I pass a parameter to a setTimeout() callback?
(29 answers)
What is the difference between a function call and function reference?
(6 answers)
Closed 3 months ago.
I was Coding Today (new) and i was wondering how people made js and python so i tried making a function based programming language using js
function init(){
log(console,"lmao ok")
setTimeout(log(console,'#--- Done'), 5000 )
}
function log(arg1,arg2) {
// body...
if (arg1 == console){
console.log(arg2)
}
else {
return
}
}
function prompt(arg1,arg2,arg3){
console.debug
var arg1 = "lmao"
}
init()
log(console,'tosay')

Extra parameters in a self executing function? [duplicate]

This question already has answers here:
What does the comma operator do in JavaScript?
(5 answers)
Closed 1 year ago.
I saw this piece of code in a webpack compiled file
(1,2, function(x) {
console.log(x);
return 4;
})(5);
This seems to execute correctly. I am aware the 5 is a parameter to function(x)
I don't understand what the 1,2 are? How is this valid?
Well, it's because the brackets only returns the function
(thing1,thing2,thing3,thing4....,thingN) would show the last thing on the list(in this case, thingN)
Example
var bracketTest1=(1,3,5,7,9,null)
console.log(bracketTest1) //null
var bracketTest2=(null,1,2,3,4)
console.log(bracketTest2) //4
var bracketTest3=(null,1,2,4,5,function(x){return x})
console.log(bracketTest3) //function(x)
console.log(bracketTest3(Object)) //function Object()
console.log((1,2,3,4,null,undefined)) //prints undefined

Does onclick = function put that function in a new scope? [duplicate]

This question already has answers here:
Why JavaScript function declaration (and expression)?
(5 answers)
Why use named function expressions?
(5 answers)
Closed 2 years ago.
Need your assistance. I have a function that was defined in onclick event and when i try to call it later there is an error that says function undefined.
author.onclick = function authorSlideInfo(event){
// do work
}
authorSlideInfo.then(() => {
// do work
}
Function returns a promise and i want to do something else after it ends. Is my function not in global scope? How to overcome this?

JS function inside a parenthesis with a parenthesis at the end with an argument, what does it mean? [duplicate]

This question already has answers here:
What is the purpose of a self executing function in javascript?
(21 answers)
Closed 6 years ago.
What does this code do? Can someone describe why the function is inside a parenthesis and also why it has a parenthesis at the end and what it is doing?
(function (innerKey) {
//doSomething
}(key));
You are creating the function and invoking it at the same time with the key value filling the innerkey parameter.
It's a self-invoking anonymous function. It will be called immediately after loading the script, and it will take the element inside the brackets key as the function's argument.
You can read more here:
What is the (function() { } )() construct in JavaScript?

what does !function() means in Javascript? [duplicate]

This question already has answers here:
What does the exclamation mark do before the function?
(8 answers)
Closed 9 years ago.
I can read and write some JavaScript, but today I came across a .js file starting with
!function() {
/*
code here
*/
}.call(window);
Question
what does ! infront of function means?
It returns "not" (!) of the result of a calling the anonymous function with a "this" of window.
Negating the result of .call(window). Not the function.

Categories

Resources