Horje
JavaScript Relational operators

JavaScript Relational Operators are used to compare their operands and determine the relationship between them. They return a Boolean value (true or false) based on the comparison result.

Types of Relational Operators

These are two types of relational operators, these are:

We will explore all the above operators along with their basic implementation with the help of examples.

JavaScript in Operator

The in-operator in JavaScript checks if a specified property exists in an object or if an element exists in an array. It returns a Boolean value.

Syntax:

prop in object

Example 1: Here is the basic example of using in operator.

JavaScript
let languages = ["HTML", "CSS", "JavaScript"];

// true (index 1 exists in the array)
console.log(1 in languages);

// false (index 3 doesn't exist in the array)
console.log(3 in languages); 

Output
true
false

Example 2: In this example, we are using in operator to check if the “name” property exists (true) and if the “address” property doesn’t exist (false).

JavaScript
const Data = {
    name: "Rahul",
    age: 21,
    city: "Noida"
};

// true ("name" property exists in the object)
console.log("name" in Data);

// false ("gender" property doesn't exist in the object)
console.log("address" in Data);

Output
true
false

JavaScript instanceof Operator

The instanceof operator in JavaScript tests if an object is an instance of a particular class or constructor, returning a Boolean value.

Syntax:

let gfg = objectName instanceof objectType

Example 1: In this example, we are using instanceof operator.

JavaScript
let languages = ["HTML", "CSS", "JavaScript"];

console.log(languages instanceof Array);
console.log(languages instanceof Object);
console.log(languages instanceof String);
console.log(languages instanceof Number);

Output
true
true
false
false

Example 2: In this example, the instanceof operator checks if myString is an instance of Object, Date, or String, and if myDate is an instance of Date, Object, or String.

JavaScript
let myString = new String();
let myDate = new Date();

console.log(myString instanceof Object);
console.log(myString instanceof Date);
console.log(myString instanceof String);
console.log(myDate instanceof Date);
console.log(myDate instanceof Object);
console.log(myDate instanceof String);

Output
true
false
true
true
true
false

JavaScript Relational operators – FAQs

How does the < (Less than) operator work?

The < operator returns true if the left operand is less than the right operand; otherwise, it returns false.

How does the > (Greater than) operator work?

The > operator returns true if the left operand is greater than the right operand; otherwise, it returns false.

How does the <= (Less than or equal to) operator work?

The <= operator returns true if the left operand is less than or equal to the right operand; otherwise, it returns false.

How does the >= (Greater than or equal to) operator work?

The >= operator returns true if the left operand is greater than or equal to the right operand; otherwise, it returns false.

How does the == (Equality) operator work?

The == operator compares two values for equality after converting both values to a common type (type coercion). It returns true if the values are equal; otherwise, it returns false.

How does the != (Inequality) operator work?

The != operator compares two values for inequality after converting both values to a common type (type coercion). It returns true if the values are not equal; otherwise, it returns false.

How does the === (Strict equality) operator work?

The === operator compares two values for equality without converting their types. It returns true if the values are equal and of the same type; otherwise, it returns false.

How does the !== (Strict inequality) operator work?

The !== operator compares two values for inequality without converting their types. It returns true if the values are not equal or not of the same type; otherwise, it returns false.




Reffered: https://www.geeksforgeeks.org


JavaScript

Related
JavaScript Program to Solve Quadratic Equation JavaScript Program to Solve Quadratic Equation
JavaScript Program to Find the Sum of Natural Numbers JavaScript Program to Find the Sum of Natural Numbers
JavaScript Program to Find Sum of Natural Numbers using Recursion JavaScript Program to Find Sum of Natural Numbers using Recursion
JavaScript Program to Convert Celsius to Fahrenheit JavaScript Program to Convert Celsius to Fahrenheit
JavaScript Program to Reverse Digits of a Number JavaScript Program to Reverse Digits of a Number

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
14