JavaScript .is()
Published Dec 22, 2023
Contribute to Docs
The .is() is a static method in JavaScript used to verify that two values are identical. This method returns a boolean value. It returns true if both values have the same type and value, and false otherwise.
Syntax
The syntax of this method can be shown as below:
Object.is(val1, val2)
It takes two parameters. val1 and val2 that represent the first and second values.
Example
The Object.is() method has a special case for -0. Although -0 is identified as a number, it is not the same as +0 or 0. While 0 and +0 are treated as the same.
let num_zero = 0,num_zplus = +0,num_zmin = -0;console.log(Object.is(num_zmin, num_zplus));console.log(Object.is(num_zmin, num_zero));console.log(Object.is(num_zero, num_zplus));
The example above will return the following output:
falsefalsetrue
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn JavaScript on Codecademy
- Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
- Includes 34 Courses
- With Professional Certification
- Beginner Friendly.115 hours
- Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
- Beginner Friendly.15 hours