Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

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

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 59

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 discusses how to implement server side field validation and display customized error messages to the client.
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

722 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