54057

What does the || mean in this suitation?

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

function getCharCount ( e,s ) { s = s || ","; return getInnerText(e).split(s).length; }

Answer1:

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.

Answer2:

That a way to define optional parameters in Javascript.

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

Answer3:

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

Answer4:

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

Answer5:

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

Answer6:

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.

Recommend

  • how to make my program to wait for a particular thread and also not affecting my GUI's interact
  • how C++ Implicitly convert c style string to a string object?
  • What distributed message queues support millions of queues?
  • CakePHP 3: change order in dateWidget
  • How can I have equal heights for inner elements of flexbox grid/boxes/cards without using jQuery?
  • jQuery Mobile - Dialogs without changing hash
  • Embedding a Google map
  • The symbol you provided is not a function
  • Why I can't use uniform1f instead of uniform4f for setting a vec4 uniform?
  • Pythons argparse default value doesn't work
  • SqlCommand back up Database
  • Cypher - matching two different possible paths and return both
  • Doctrine2 bulk import try to work with another entity
  • Passing “get” parameters doesn't work, parameter not visible in the link
  • blade.php method outputting it's result to the form
  • Scipy Leastsq Optional Output Variable (Mesg)
  • xcode don't localize specific strings
  • converting text file into xml using php?
  • Declaring variable dynamically in VB.net
  • NetLogo BehaviorSpace - Measure runs using reporters
  • Spring security and special characters
  • HTML download movie download link
  • Submit form in a displaytag pagination
  • Updating server-side rendering client-side
  • JSON with duplicate key names losing information when parsed
  • Alternatives to the OPTIONAL fallback SPARQL pattern?
  • Jquery - Jquery Wysiwyg return html as a string
  • Arrays break string types in Julia
  • WPF Applying a trigger on binding failure
  • C# - Getting references of reference
  • How to set the response of a form post action to a iframe source?
  • Angular 2 constructor injection vs direct access
  • Java static initializers and reflection
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • Change div Background jquery
  • Android Google Maps API OnLocationChanged only called once
  • How does Linux kernel interrupt the application?
  • Busy indicator not showing up in wpf window [duplicate]
  • UserPrincipal.Current returns apppool on IIS
  • Why do underscore prefixed variables exist?