Solved

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

Posted on 2016-09-07
3
38 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 51

Accepted Solution

by:
Julian Hansen earned 500 total points
Comment Utility
&& 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
Comment Utility
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 51

Expert Comment

by:Julian Hansen
Comment Utility
You are welcome.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now