Solved

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

Posted on 2016-09-07
3
88 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 58

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 58

Expert Comment

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

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
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…

630 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