

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Why it is better not to create numbers as objects in JavaScript?
In javascript try not to convert numbers into objects because numbers can't be compared to objects and even objects cannot be compared to objects.
Example
In the following example, the provided number 20 is assigned to both variable 'x' and variable 'y'. When compared both the variables with each other, Boolean value "true" is displayed as shown in the output.
<html> <body> <script> var x = 500; var y = (500); document.write((x===y)); document.write("</br>"); document.write(typeof(x)); document.write("</br>"); document.write(typeof(y)); </script> </body> </html>
Output
true number number
Example
In the following example, variable "y "is turned from number to an object and then when compared with variable "x" Boolean value false is displayed as shown in the output.
<html> <body> <script> var x = 500; var y = new Number(500); document.write((x===y)); document.write("</br>"); document.write(typeof(x)); document.write("</br>"); document.write(typeof(y)); </script> </body> </html>
Output
false number object
- Related Questions & Answers
- Why are numbers represented as objects in python?
- Why is it important to update all the software to have better security?
- Create many JavaScript objects as the same type?
- Why is it important to create a Data Backup?
- What is AMBUSH Marketing? Why is it known as surprise attack?
- Why Qries is a better platform compared to Quora?
- Why is it not recommended to use the mixture of quoted as well as unquoted values in MySQL IN() function’s list?
- Why is Greenland called as island and not a continent?
- Is it better to have one big JavaScript file or multiple light files?
- Why bing image search is better than others
- How to create objects in JavaScript?
- Why is Greenland called as an island and not a continent?
- Why is it necessary to declare NOT FOUND handler while using MySQL cursor?
- Why is it not good to sleep on the stomach for the whole night?
- Why is it good to write the numbers in MySQL INTERVAL() function in ascending order?
Advertisements