0

I am new to javascript and this is my function:

var now = any(function(result){
    console.log(result);

})


function any(gone){
    return gone
}

console.log(now)

but if I give it params like var now = any(function(result){ it returns me tada.. what I want is it should take default value null or None like in python and if the value is not provide it should give null or None

function any(gone=null) somthing like this.. after that I want to receive callback in result

5
  • function any(gone){ return gone(); } Commented Feb 1, 2016 at 6:29
  • 2
    "now" is a function instead of the return value of that function Commented Feb 1, 2016 at 6:29
  • What is the expected result? What should be logged, which value should now get? Commented Feb 1, 2016 at 6:37
  • Prior to ECMAScript 2015 you must test the value of parameters and assign values based on the outcome, see MDN: Default parameters Commented Feb 1, 2016 at 6:41
  • 1
    Please state clearly the problem you are trying to solve, or the desired behavior you hope to achieve. Commented Feb 1, 2016 at 7:15

1 Answer 1

1
function any(gone){

   gone = gone || null;

}

And in ES6 you can do

function any(gone = whatever){

       gone = gone || null;

}
Sign up to request clarification or add additional context in comments.

2 Comments

doing null it gives me [Function] as response
Though you might want a more explicit test like if (typeof gone == 'undefined') since any(0) or any(false) likely should not replace the value with null.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.