This question already has answers here:
Using ternary operator in JavaScript to invoke two functions
(2 answers)
Closed 5 years ago.
I want a shorthand that runs a function if true and runs a separate function if false. Something like:
(condition)? function1:function2;
condition? function1():function2();
function a() {
return "a";
}
function b() {
return "b";
}
(1 == 2) ? alert(a()) : alert(b());
Related
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')
This question already has answers here:
Using '&&' as a shorthand if statement?
(5 answers)
Javascript usage of && operator instead of if condition
(7 answers)
"if" shorthand with double ampersand
(4 answers)
Closed 1 year ago.
Let me get to this point straight. So I found a shorthand on an Instagram post for something like this:
const bool = true
function myFunction (){
//smth
}
if(bool){
myFunction
}
I can't remember what it was, something with the && operator. Anyone?
sorry for being a bit off topic but I really need this.
Perhaps you're thinking of just using && as it short circuits
const bool = true;
const notBool = false;
function myFunction(){ console.log("hello, world!") };
bool && myFunction();
notBool && myFunction(); // nada!
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
This question already has answers here:
Variadic curried sum function
(19 answers)
Currying in javascript for function with n parameters
(5 answers)
Closed 5 years ago.
How do I write a function which can be called several times in a row with one parameter? Here is an example:
function sum(a) {
// what should here?
}
sum(1)(2) // should return 3
Return another function that when called sums the argument of the first and the second like this:
function sum(a) {
return b => a + b
}
console.log(sum(1)(2))
This question already has answers here:
How do JavaScript closures work?
(86 answers)
Closed 5 years ago.
If I write something like below
function sum(x){
return function(y){
return function(z){
return x + y + z;
}
}
}
and call it like sum(2)(3)(4) // output is 8
can we call the above function, an example of closure within a closure ???
Yes, it is an example of closure within a closure.