Solved

How do the JavaScript &&  !== operators work together?

Posted on 2016-09-07
3
62 Views
Last Modified: 2016-09-07
function() {
return '{{visitor.id}}' && '{{visitor.id}}' !== 'undefined' ? '{{visitor.id}}' : undefined;
}

Open in new window


Hi - I'm trying to debug some code and don't know JavaScript well enough to fully understand this piece of code. But from what I can gather, I believe this piece of code says (in laymens terms)
Return visitor.id if visitor.id is undefined

Is this correct? If not, could you help me understand what this is doing?
0
Comment
  • 2
3 Comments
 
LVL 55

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 41788510
&& is a boolean operator that returns true if both expressions left and right are true

!== is a strict comparison operator

0, false and null all evaluate to false but !== false checks specifically for the false value (0, null will not match)

return '{{visitor.id}}' && '{{visitor.id}} !== 'undefined'

Open in new window

This looks like a template expression from Angular or similar. In a strict JavaScript sense '{{vistor.id}}' is just a string so will evaluate to true
I am guessing there is some compiling of that expression that is going on.

The expression is using a ternary operator (?) which is a short version of if (expr) then ... else...

Assuming that the {{visitor.id}} strings are being evaluated to the value of the id property of the visitor object then the return value is checking that
visitor.id is not a false value and it is not equal to undefined then return its value otherwise return undefined.

Seems like a waste of CPU cycles to me - a long winded way of saying return visitor.id but without seeing the context I cannot say.

To answer your question - no it will always return a value and from what I can see it will be whatever visitor.id is.
0
 

Author Comment

by:Elizabeth "Smalls" Eckels
ID: 41788518
This was a very good explanation, thank you so much.

The context of the expression was to prevent an analytics system from overwriting a Visitor ID (such as ABC123) with "false" or "undefined" on web pages where it was false or undefined. The analytics system itself can't be configured to ignore undefined or false values, so that was the intent of this expression. I just didn't understand if, how or why it worked...but now I do. Thanks Julian!
0
 
LVL 55

Expert Comment

by:Julian Hansen
ID: 41788559
You are welcome.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question