JavaScript Conditions Use Cases

This Article Will Help You:


JavaScript is the most widely used scripting language on earth. Javascript can be used with Convert in a diversity of cases. This article is all about JS conditions examples that can be used targeting URLs, cookies, variables, and others. The codes are from real use case scenarios.

Where Javascript can be implemented?

Basically everywhere, Goals, Audiences, Locations, Experience, Variation, and Project.

Comparison Operators

Case: x = 5 and y = 3

Operator Description Comparing Returns
== equal to x == 8 false
x == 5 true
=== equal value and equal type x === "5" false
x === 5 true
!= not equal x != 8 true
!== not equal value or not equal type x !== "5" true
x !== 5 false
> greater than x > 8 false
< less than x < 8 true
>= greater than or equal to x >= 8 false
<= less than or equal to x <= 8 true
&& and (x < 10 && y > 1)  is true
|| or (x === 5 || y === 5) is false

source: https://www.w3schools.com/


Examples:

Referral URL

// Equal
document.referrer == "YourURL.com"


// Not Equal
document.referrer != "YourURL.com"


// Contains
document.referrer.includes('somethingtocheck') == true/false

URL/Parameter/Query String :: Contains

window.location.href.includes('yourstring') == true

window.location.href.includes('\?yourstring') || window.location.href.includes('\&yourstring')  == true

window.location.href.includes('yourstring') && window.location.href.includes('yourstring2')  == true

URL using Regex

//Starts with
window.location.href.match(/https?:\/\/(www.yourdomain.com\//).length > 0

//Using wildcards (same as contains)
window.location.href.match(/(.*)\/(child\/child1\/child2)(|\/)(.*)/).length > 0

Cookies

//Cookie is present
document.cookie.indexOf('cookieName') > -1 

//Cookie is not present
document.cookie.indexOf('cookieValue') > -1 == false

// Cookie is present and contains
document.cookie.indexOf('cookieName') > -1 && document.cookie.indexOf('cookieValue') > -1

Variables

//Variable is present
typeof yourvariable != 'undefined'

//Variable is not present
typeof yourvariable == 'undefined'

//Variable contains/not contains
yourvariable.includes('valuetocheck') == true/false

Elements

//Element is present
document.getElementById("intro") != null
document.getElementsByClassName("intro") != null
convert.$('.selector').length != 0

//Element is not present
document.getElementById("intro") == null
document.getElementsByClassName("intro") == null
convert.$('.selector').length == 0

Screen Size

screen.width == 1920
screen.height == 1080

Min/Max Screen Size 

window.matchMedia("(min-width: 768px)").matches
window.matchMedia("(max-width: 1024px)").matches