Skip to content Skip to sidebar Skip to footer

"||" Operator Is Not Working When Assigning Value To A Variable

What is wrong with this code? var sha = 6; var secondParameter = dan || sha; alert(secondParameter); I tried it with many browsers. No alert. If I add var dan like this: var sha

Solution 1:

You don't have the dan defined. Execution stops at that error. Check the browser console for errors.

When you define dan, execution continues, but that's probably not what you want.

The purpose of such code in JavaScript is to say if dan has a falsy value (any value that evaluates to a false value, i.e. 0, '', null, false, undefined, or NaN), then use the value of sha.

IF defining dan is beyond your responsibility, (i.e some other script should have set it), then you can check for its existence using a construct like follows:

secondParameter = typeof(dan) == 'undefined' ? sha : dan;

(Not tested, hopefully it works. :) anyways, it should give you an idea)

Check here for documentation on logical operators.

Also, this question might give you more insight: Javascript || or operator with a undefinded variable


Solution 2:

var sha = 6;
var dan = null;
var secondParameter = dan || sha;
alert(secondParameter);

Try this. You can coalesce using the || operator, but all variables have to be declared in order to be referenced


Solution 3:

This is used in a situation where it is given as something like a parameter but isn't assigned a value or it is falsy

falsy being 0, false, null or undefined

For example:

var sha = 6;
var dan = undefined;
var secondParameter = dan || sha;
alert(secondParameter);

You will get 6

A good example to use this would be something like this:

function funcWithCallback(callback){
     callback = callback || function(){};
     callback();
}

In this example if callback is undefined (or falsy) then set callback as a new instance of a function

Using it in a situation like this will ensure no error


Solution 4:

There is a difference in how JavaScript handles undeclared variables (undefined variable) and undefined values. In the following example the variable dan is declared but never set so it's value is undefined the || returns the first true value it can find so if I pass anything but an empty string, 0, false, NaN, undefined or NULL it'll console.log the passed value. Else it'll log "default value".

function test(dan){
  console.log(dan || "default value");
  return dan===undefined;
}
console.log(test());//default value, then true
console.log(test(22));//22 then false

A more robust way of checking if a variable was passed would be to see if the variable's value is undefined:

function test(dan){
  dan = (typeof(dan)==='undefined')?"default value":dan;
}

In your example the variable dan is not declared at all (variable is undefined), that's why you get error "dan is not defined" because dan is not declared at all.

function test(){
  return dan===undefined;//ReferenceError: dan is not defined
}
console.log(test());

You could change your code to this:

var sha = 6, dan;//dan is declared here but no value is set
var secondParameter = dan || sha;
console.log(dan===undefined);//true
console.log(secondParameter);//6

If you want to check if a certain object has a property then it'll not throw an error:

function test(){
  return window.neverSetOrDeclared===undefined;
}
console.log(test());//true

It will throw an error when you try to check a property of undefined or null:

null.something//throws error
undefined.something//throws error
window.neverSetOrDeclared===undefined//throws error

Solution 5:

Explanation: the way this || works is : if first condition is false then it checks second condition. So in order to display second parameter, you'll have to make first parameter null.


Post a Comment for ""||" Operator Is Not Working When Assigning Value To A Variable"