Javascript less or equal

Less than or equal (<=)

The operands are compared using the same algorithm as the Less than operator, with the operands swapped and the result negated. x y are both false :

  • If one of the operands gets converted to a BigInt, while the other gets converted to a string that cannot be converted to a BigInt value (it throws a syntax error when passed to BigInt() ).
  • If one of the operands gets converted to NaN . (For example, strings that cannot be converted to numbers, or undefined .)
    When one of x or y is null , and the other is something that’s not null and becomes 0 when coerced to numeric (including 0 , 0n , false , «» , «0» , new Date(0) , etc.): x
    When one of x or y is undefined , and the other is one of null or undefined : x
    When x and y are the same object that becomes NaN after the first step of Less than (such as new Date(NaN) ): x
    When x and y are different objects that become the same value after the first step of Less than: x

Examples

String to string comparison

"a"  "b"; // true "a"  "a"; // true "a"  "3"; // false 

String to number comparison

"5"  3; // false "3"  3; // true "3"  5; // true "hello"  5; // false 5  "hello"; // false 

Number to Number comparison

5  3; // false 3  3; // true 3  5; // true 

Number to BigInt comparison

5n  3; // false 3  3n; // true 3  5n; // true 

Comparing Boolean, null, undefined, NaN

true  false; // false true  true; // true false  true; // true true  0; // false true  1; // true null  0; // true 1  null; // false undefined  3; // false 3  undefined; // false 3  NaN; // false NaN  3; // false 

Specifications

Browser compatibility

BCD tables only load in the browser

See also

Found a content problem with this page?

This page was last modified on Mar 30, 2023 by MDN contributors.

Your blueprint for a better internet.

Источник

Comparison Operators

You know comparison operators from math class, but let’s refresh your knowledge:

Greater than operator (a > b) returns true if the left operand is greater than the right operand.

Syntax:

Example of the greater than operator:

w3docs logo

Javascript Greater than operator

Less than operator (a

Example of the less than operator:

w3docs logo

Javascript less than operator

Greater than or equal operator (a >= b) returns true if the left operand is greater than or equal to the right operand.

Syntax:

Example of the greater than or equal operator:

w3docs logo

jJvascript Greater than or equal operator

Less than or equal operator (a

Example of the less than or equal operator:

w3docs logo

Javascript less than or equal operator

The equality operator (a == b) converts the operands if they are not of the same type, then applies strict comparison. If both operands are objects, JavaScript compares internal references which are equal when operands refer to the same object in memory.

Syntax:

Example of the equality operator:

w3docs logo

Javascript equality operator

console.log(1 == 1); // true console.log(«2» == 2); // true console.log(3 == ‘3’); // true console.log(0 == false); // true console.log(0 == null); // false console.log(0 == undefined); // false console.log(null == undefined); // true

If the operands are not equal, inequality operator (!=) returns true. When two operands are not of the same type, JavaScript tries to convert the operands to an appropriate type for the comparison.

If both operands are objects, JavaScript compares references that are not equal, when operands refer to different objects in memory.

Syntax:

Example of the inequality operator:

w3docs logo

Javascript inequality operator

console.log(1 != 2) ; // true console.log(2 != «2»); // false console.log(3 != ‘3’ ); // false console.log(1 != true); // false console.log(0 != false); // false

Boolean is the result

A comparison returns a value like all other operators. The value, in this case, is a boolean.

w3docs logo

Comparison operator in javascript

console.log( 3 > 1 ); // true (correct) console.log( 3 == 1 ); // false (wrong) console.log( 3 != 1 ); // true (correct)

A comparison result can be assigned to a variable, like any value:

w3docs logo

Comparison operator javascript

String comparison

JavaScript uses “dictionary” or “lexicographical” order to see if one string is greater than another. In other words, strings are compared letter-by-letter.

Example of the string comparison:

w3docs logo

The string comparison in javascript

console.log( ‘Z’ > ‘A’ ); // true console.log( ‘Want’ > ‘Walk’ ); // true console.log( ‘Too’ > ‘To’ ); // true

In the examples the comparison ‘Z’ > ‘A’ gets to a result at the first step while the strings «Want» and «Walk» are compared character-by-character:

Comparison of different types

For comparing values of different types, JavaScript converts the values to numbers.

w3docs logo

The comparison of different types in javascript

console.log( ‘3’ > 1 ); // true, string ‘3’ becomes a number 3 console.log( ’02’ == 2 ); // true, string ’02’ becomes a number 2

For boolean values, true becomes 1, false becomes 0.

w3docs logo

Comparison of different types in javascript

Strict equality

A regular equality check == has a problem, as it can’t differentiate 0 from false:

w3docs logo

The regular equality == in javascript

We meet the same thing with an empty string:

w3docs logo

The regular equality == in javascript

Comparison with null and undefined

Non-intuitive behavior is when null or undefined are compared to other values. For a strict equality check ===. These values are different, as each of them is a different type.

w3docs logo

The strict equality === in javascript

For a non-strict check ==

These two are a “sweet couple”, it means that they equal each other.

w3docs logo

The regular equality == in javascript

The Difference between == and ===

JavaScript has two visually similar, but very different ways to test equality:

== (Double equals operator): the equality or abstract comparison operator

=== (Triple equals operator): the identity or strict comparison operator

Here are the differences between == and ===:

  • before showing comparison == converts the variable values of the same type;
  • === does not do any type conversion and returns true only if both values and types are identical for the two variables being compared.

w3docs logo

== and === equality in javascript

var val1 = 13; var val2 = 13; console.log(val1 == val2); // true console.log(val1 === val2); // also true

For maths and other comparisons =

Null/undefined are converted to numbers, here null becomes 0, undefined becomes NaN.

We present you some funny things that happen when we apply these rules. And also, how to not fall into a trap with them.

Strange result: null vs 0

Example of the compare null with a zero:

w3docs logo

Compare null with a zero in javascript

console.log( null > 0 ); // (1) false console.log( null == 0 ); // (2) false console.log( null >= 0 ); // (3) true

Mathematically, that’s strange. The last result says that «null is greater than or equal to zero», it means that in one of the comparisons above it must be true, but both of them are false.

The main reason is that an equality check == and comparisons > = = 0 is true and (1) null > 0 is false.

But on the other hand, the equality check == for undefined and null is defined without any conversions. They equal each other and don’t equal anything else, that’s why (2) null == 0 is false.

An incomparable undefined

We can’t compare the value undefined to other values:

w3docs logo

Compare the value with undefined in javascript

console.log( undefined > 0 ); // false (1) console.log( undefined < 0 ); // false (2) console.log( undefined == 0 ); // false (3)

The reasons why we get these results are:

  • Comparisons (1) and (2) return false, as undefined gets converted to NaN, it’s a special numeric value which returns false for all comparisons.
  • The equality check (3) returns false, as here undefined only equals null, undefined and no other value.

Источник

Expressions and operators

This chapter documents all the JavaScript language operators, expressions and keywords.

Expressions and operators by category

For an alphabetical listing see the sidebar on the left.

Primary expressions

Basic keywords and general expressions in JavaScript. These expressions have the highest precedence (higher than operators).

The this keyword refers to a special property of an execution context.

Basic null , boolean, number, and string literals.

Array initializer/literal syntax.

Object initializer/literal syntax.

The function keyword defines a function expression.

The class keyword defines a class expression.

The function* keyword defines a generator function expression.

The async function defines an async function expression.

The async function* keywords define an async generator function expression.

Regular expression literal syntax.

Left-hand-side expressions

Left values are the destination of an assignment.

Member operators provide access to a property or method of an object ( object.property and object[«property»] ).

The optional chaining operator returns undefined instead of causing an error if a reference is nullish ( null or undefined ).

The new operator creates an instance of a constructor.

In constructors, new.target refers to the constructor that was invoked by new .

An object exposing context-specific metadata to a JavaScript module.

The super keyword calls the parent constructor or allows accessing properties of the parent object.

The import() syntax allows loading a module asynchronously and dynamically into a potentially non-module environment.

Increment and decrement

Postfix/prefix increment and postfix/prefix decrement operators.

Postfix increment operator.

Postfix decrement operator.

Prefix increment operator.

Prefix decrement operator.

Unary operators

A unary operation is an operation with only one operand.

The delete operator deletes a property from an object.

The void operator evaluates an expression and discards its return value.

The typeof operator determines the type of a given object.

The unary plus operator converts its operand to Number type.

The unary negation operator converts its operand to Number type and then negates it.

Pause and resume an async function and wait for the promise’s fulfillment/rejection.

Arithmetic operators

Arithmetic operators take numerical values (either literals or variables) as their operands and return a single numerical value.

Relational operators

A comparison operator compares its operands and returns a boolean value based on whether the comparison is true.

Less than or equal operator.

Greater than or equal operator.

The instanceof operator determines whether an object is an instance of another object.

The in operator determines whether an object has a given property.

Note: => is not an operator, but the notation for Arrow functions.

Equality operators

The result of evaluating an equality operator is always of type boolean based on whether the comparison is true.

Strict inequality operator.

Bitwise shift operators

Operations to shift all bits of the operand.

Bitwise left shift operator.

Bitwise right shift operator.

Bitwise unsigned right shift operator.

Binary bitwise operators

Bitwise operators treat their operands as a set of 32 bits (zeros and ones) and return standard JavaScript numerical values.

Binary logical operators

Logical operators implement boolean (logical) values and have short-circuiting behavior.

Nullish Coalescing Operator.

Conditional (ternary) operator

The conditional operator returns one of two values based on the logical value of the condition.

Assignment operators

An assignment operator assigns a value to its left operand based on the value of its right operand.

Unsigned right shift assignment.

Nullish coalescing assignment.

Destructuring assignment allows you to assign the properties of an array or object to variables using syntax that looks similar to array or object literals.

Yield operators

Pause and resume a generator function.

Delegate to another generator function or iterable object.

Spread syntax

Spread syntax allows an iterable, such as an array or string, to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected. In an object literal, the spread syntax enumerates the properties of an object and adds the key-value pairs to the object being created.

Comma operator

The comma operator allows multiple expressions to be evaluated in a single statement and returns the result of the last expression.

Specifications

Browser compatibility

BCD tables only load in the browser

See also

Found a content problem with this page?

This page was last modified on Apr 5, 2023 by MDN contributors.

Your blueprint for a better internet.

MDN

Support

Our communities

Developers

Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.

Источник

Читайте также:  Css circle with border radius
Оцените статью