2

Typically, || means or, but what does it mean in this case:

function getCharCount ( e,s ) {
    s = s || ",";
    return getInnerText(e).split(s).length;
}      
soulcheck
  • 36,297
  • 6
  • 91
  • 90
Tattat
  • 15,548
  • 33
  • 87
  • 138
  • Can someone please clarify. There are upvoted answers that say that if s is null then comma and another upvoted if s is null or "" then comma. – Barry Kaye Jul 23 '11 at 16:17
  • 2
    @Barry: it's because people love clicking arrows! Raynos answer coupled with the comment is the most correct. Terminology wise, it ends up being a coalesce type operator instead of a logical or. – Marc Jul 23 '11 at 16:28
  • possible duplicate of [What does this construct (x = x || y) mean?](http://stackoverflow.com/questions/2802055/what-does-this-construct-x-x-y-mean) – Felix Kling Feb 10 '13 at 00:25

6 Answers6

10

s = s || ","

It's the default parameter option. If s is "falsey" s will be set to ","

So if s is an "" or undefined it will have a useful default.

Raynos
  • 166,823
  • 56
  • 351
  • 396
0

That a way to define optional parameters in Javascript.

You can call this function with only 1 param...

ChristopheCVB
  • 7,269
  • 1
  • 29
  • 54
0

Raynos has provided the answer, but there is more to add to describing this.

s = s || ",";

If s is any falsey value such as undefined, null, 0, false, NaN, "", etc..., then s will get initialized to be ",".

This can be very useful for initializing optional parameters to a function or for guaranteeing that parameters have at least some initial value. One does have to be very careful how it is used because this construct prohibits purposely passing a falsey value for the parameter s.

In this example, you can't pass an empty string for the value of s because it will be changed into ",". That's OK in this function, but maybe not in others. In other types of functions, you wouldn't be able to pass false which might be an allowable value.

If one only wants to protect against the parameter not being passed, then you have to use something like this which explicitly tests for undefined and allows the passing of other falsey values:

s = typeof s != "undefined" ? s : ',';    // if s is undefined, initialize

or if you want to validate that it was a string and allow an empty string, you could use:

s = typeof s == "string" ? s : ',';       // if s not a string, initalize
jfriend00
  • 683,504
  • 96
  • 985
  • 979
-1

It means that s, if it's null, gets assigned the value ","; if s is not null, it retains its value.

Roy Dictus
  • 32,551
  • 8
  • 60
  • 76
-1

s is s, or if s is not defined ( you did not pass the argument in the function call ), it's ","

Willem
  • 723
  • 2
  • 9
  • 27
  • Any value of s that is falsey (undefined, false, 0, "", null, ...) will trigger the assignment of the comma string, not just undefined. I didn't downvote, but wanted to explain what was missing from your answer. – jfriend00 Jul 23 '11 at 16:48
  • If you wanted the statement to trigger only when s was undefined, it would have to be something like this: `s = typeof s != "undefined" ? s : ',';` – jfriend00 Jul 23 '11 at 16:53
-1

sometimes , you dont know if a variable will ahve a value so you are telling your self that you have to have a value.

if if theres no value ( null) so youll have your own default value.

that what you wrote

 s = s || ",";

i dont know if s has value or not , but if not so put the ',' value.

Raynos
  • 166,823
  • 56
  • 351
  • 396
Royi Namir
  • 144,742
  • 138
  • 468
  • 792