Skip to content
-
Difference between != and !== operator in JavaScript
Last Updated :
29 Dec, 2023
!= operator
The inequality operator (!=) is the logical opposite of the equality operator. It means "Not Equal" and returns true whereas equality would return false and vice versa. Like the equality operator, the inequality operator will convert data types of values while comparing.
For example 1 != '1' will return false since data type conversion takes place so 1 and '1' are considered equal.
Example: Below example shows != Operator.
JavaScript
console.log(" 1 != '1' " + (1 != '1'));
console.log(" 1 != 1 " + (1 != 1));
console.log(" 1 != '2' " + (1 != '2'));
Output 1 != '1' false
1 != 1 false
1 != '2' true
!== operator
The strict inequality operator (!==) is the logical opposite of the strict equality operator. It means "Strictly Not Equal" and returns true whereas strict equality would return false and vice versa.
Strict inequality will not convert data types.
For example 1 !== '1' will return true since 1 is an integer and '1' is a character and no data type conversion takes place.
Example: Below example shows !== Operator.
JavaScript
console.log(" 1 !== '1' " + (1 !== '1'));
console.log(" 1 !== 1 " + (1 !== 1));
console.log(" 1 !== '2' " + (1 !== '2'));
Output 1 !== '1' true
1 !== 1 false
1 !== '2' true
Difference between != and !== operator in JavaScript
Feature | != (Loose Inequality) | !== (Strict Inequality) |
Type Coercion | Converts values to the same type for comparison. | Compares values without changing their types. |
Example | 5 != "5" is false | 5 !== "5" is true |
Usage Recommendation | Use with caution as it may lead to unexpected results due to type coercion. | Preferred in most cases to ensure both value and type are considered. It's more explicit and reduces the chance of unexpected behavior. |
Similar Reads
Difference Between && and || Operators in javaScript In JavaScript, logical operators && (AND) and || (OR) are used to perform the logical operations on values. These operators are commonly used in the control flow and decision-making within the programs. Understanding the differences between these operators is essential for writing efficient
2 min read
Difference Between == & === in JavaScript In JavaScript, comparison operators like == (double equals) and === (triple equals) are used to compare values, but they behave differently. The == operator is known as the loose equality operator. It compares two values for equality after performing type coercion, meaning it converts the values to
3 min read
Difference Between == & === in JavaScript In JavaScript, comparison operators like == (double equals) and === (triple equals) are used to compare values, but they behave differently. The == operator is known as the loose equality operator. It compares two values for equality after performing type coercion, meaning it converts the values to
3 min read
Difference between the (=), (==), and (===) operators in PHP In PHP, the '=' operator is used for assignment, while the '==' operator is used for loose equality comparison, meaning it checks if two values are equal without considering their data types. On the other hand, the '===' operator is used for strict equality comparison, meaning it checks if two value
3 min read
What is the difference between == and === in PHP ? In this article, we will discuss the differences between '==' and '===' operators in PHP. Both are comparison operators used to compare two or more values. == Operator: This operator is used to check the given values are equal or not. If yes, it returns true, otherwise it returns false. Syntax: oper
2 min read
Difference between a || b < 0 and a < 0 || b < 0 in JavaScript ? Both expression almost looks the same when we focused on the || (Or) operator, but both expressions are different from each other. To know the final conclusion, we have to get the knowledge of the || (Or) operator first. JavaScript || (Or) Operator: The âORâ operator is the opposite of the âANDâ ope
2 min read
`;
$(commentSectionTemplate).insertBefore(".article--recommended");
}
loadComments();
});
});
function loadComments() {
if ($("iframe[id*='discuss-iframe']").length top_of_element && top_of_screen articleRecommendedTop && top_of_screen articleRecommendedBottom)) {
if (!isfollowingApiCall) {
isfollowingApiCall = true;
setTimeout(function(){
if (loginData && loginData.isLoggedIn) {
if (loginData.userName !== $('#followAuthor').val()) {
is_following();
} else {
$('.profileCard-profile-picture').css('background-color', '#E7E7E7');
}
} else {
$('.follow-btn').removeClass('hideIt');
}
}, 3000);
}
}
});
}
$(".accordion-header").click(function() {
var arrowIcon = $(this).find('.bottom-arrow-icon');
arrowIcon.toggleClass('rotate180');
});
});
window.isReportArticle = false;
function report_article(){
if (!loginData || !loginData.isLoggedIn) {
const loginModalButton = $('.login-modal-btn')
if (loginModalButton.length) {
loginModalButton.click();
}
return;
}
if(!window.isReportArticle){
//to add loader
$('.report-loader').addClass('spinner');
jQuery('#report_modal_content').load(gfgSiteUrl+'wp-content/themes/iconic-one/report-modal.php', {
PRACTICE_API_URL: practiceAPIURL,
PRACTICE_URL:practiceURL
},function(responseTxt, statusTxt, xhr){
if(statusTxt == "error"){
alert("Error: " + xhr.status + ": " + xhr.statusText);
}
});
}else{
window.scrollTo({ top: 0, behavior: 'smooth' });
$("#report_modal_content").show();
}
}
function closeShareModal() {
const shareOption = document.querySelector('[data-gfg-action="share-article"]');
shareOption.classList.remove("hover_share_menu");
let shareModal = document.querySelector(".hover__share-modal-container");
shareModal && shareModal.remove();
}
function openShareModal() {
closeShareModal(); // Remove existing modal if any
let shareModal = document.querySelector(".three_dot_dropdown_share");
shareModal.appendChild(Object.assign(document.createElement("div"), { className: "hover__share-modal-container" }));
document.querySelector(".hover__share-modal-container").append(
Object.assign(document.createElement('div'), { className: "share__modal" }),
);
document.querySelector(".share__modal").append(Object.assign(document.createElement('h1'), { className: "share__modal-heading" }, { textContent: "Share to" }));
const socialOptions = ["LinkedIn", "WhatsApp","Twitter", "Copy Link"];
socialOptions.forEach((socialOption) => {
const socialContainer = Object.assign(document.createElement('div'), { className: "social__container" });
const icon = Object.assign(document.createElement("div"), { className: `share__icon share__${socialOption.split(" ").join("")}-icon` });
const socialText = Object.assign(document.createElement("span"), { className: "share__option-text" }, { textContent: `${socialOption}` });
const shareLink = (socialOption === "Copy Link") ?
Object.assign(document.createElement('div'), { role: "button", className: "link-container CopyLink" }) :
Object.assign(document.createElement('a'), { className: "link-container" });
if (socialOption === "LinkedIn") {
shareLink.setAttribute('href', `https://www.linkedin.com/sharing/share-offsite/?url=${window.location.href}`);
shareLink.setAttribute('target', '_blank');
}
if (socialOption === "WhatsApp") {
shareLink.setAttribute('href', `https://api.whatsapp.com/send?text=${window.location.href}`);
shareLink.setAttribute('target', "_blank");
}
if (socialOption === "Twitter") {
shareLink.setAttribute('href', `https://twitter.com/intent/tweet?url=${window.location.href}`);
shareLink.setAttribute('target', "_blank");
}
shareLink.append(icon, socialText);
socialContainer.append(shareLink);
document.querySelector(".share__modal").appendChild(socialContainer);
//adding copy url functionality
if(socialOption === "Copy Link") {
shareLink.addEventListener("click", function() {
var tempInput = document.createElement("input");
tempInput.value = window.location.href;
document.body.appendChild(tempInput);
tempInput.select();
tempInput.setSelectionRange(0, 99999); // For mobile devices
document.execCommand('copy');
document.body.removeChild(tempInput);
this.querySelector(".share__option-text").textContent = "Copied"
})
}
});
// document.querySelector(".hover__share-modal-container").addEventListener("mouseover", () => document.querySelector('[data-gfg-action="share-article"]').classList.add("hover_share_menu"));
}
function toggleLikeElementVisibility(selector, show) {
document.querySelector(`.${selector}`).style.display = show ? "block" : "none";
}
function closeKebabMenu(){
document.getElementById("myDropdown").classList.toggle("show");
}