Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2016-09-07
3
Medium Priority
?
99 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 60

Accepted Solution

by:
Julian Hansen earned 2000 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 60

Expert Comment

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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article was originally published on Monitis Blog, you can check it here . Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime…
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Suggested Courses

886 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