jeudi 13 août 2015

If argument is blank do not append to string

I want to write a simple javascript function to append to a string of values given arguments, but only if the arguments have values. Here is an example:

function foo(bar){
  return "hello" + bar;  
}

If I run foo() I will get "helloundefined" I want it to just return "hello" and if i run foo('world') that works right now with "helloworld"

I was thinking I could do something like:

return "hello" + null || bar but i would just get "hellonull"

or

return "hello" + if(bar){bar} is invalid syntax.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire