What is differnce between == and === in programing

By toswebdev, 10 February, 2023
What is differnce between == and === in programing

In many programming languages, == and === are used to compare values to determine whether they are equal. The difference between == and === depends on the programming language, but in general, == is used for equality comparison that performs type coercion, while === is used for strict equality comparison that does not perform type coercion.

For example, in JavaScript, == will return true if the values being compared are equal after type coercion, while === will return true only if the values are equal and have the same type. For example:

1 == "1" // true (1 is coerced to "1")
1 === "1" // false (different types)

In PHP, == will return true if the values being compared are equal after type coercion, while === will return true only if the values are equal, have the same type, and are of the same value.

In other programming languages, such as Python, there is only one equality operator, ==, which performs equality comparison without type coercion.

In summary, the difference between == and === is that == performs an equality comparison that may involve type coercion, while === performs a strict equality comparison that does not perform type coercion.

Sure, here are examples of == and === comparison in various programming languages:

JavaScript:


console.log(1 == "1"); // true (type coercion)
console.log(1 === "1"); // false (different types)
console.log(true == 1); // true (type coercion)
console.log(true === 1); // false (different types)

Python:

print(1 == "1") # False (different types)

Ruby:

puts 1 == "1" # false (different types)
puts 1 === "1" # false (different types)

PHP:

var_dump(1 == "1"); // bool(true) (type coercion)
var_dump(1 === "1"); // bool(false) (different types)

Java:

System.out.println(1 == "1"); // compiler error (incompatible types)

C#:

Console.WriteLine(1 == "1"); // compiler error (incompatible types)

In some programming languages, the === operator may not exist, and only the == operator is used for both type-coercive and strict equality comparisons. In these cases, it is important to understand how the == operator behaves in that particular language.

Comments

All comments go through moderation, so your comment won't display immediately.