Everything in JavaScript is an object, true or false?

8

In several responses on this site I have read contradictory statements about whether everything in JavaScript is an object or not.

What is the truth?

The idea is that the answers are based on reliable sources, preferably in the ECMAScript 2016 (version 7) but it may well be that a specification element has been introduced in this regard.

Here are a couple of examples of each case

Everything is an object

Not everything is an object

Ambiguous

General note:

Although this question has an answer from me, the idea is to have the best explanation on this for the benefit of the community.

Specific notes:

1 : This question has been marked as duplicate because it was thought that the answer to this question is in the answer, however, it is precisely pointing out the controversy that is considering the answers to other questions. On the other hand, this response does not base its arguments on external sources.

    
asked by Rubén 10.07.2017 в 02:54
source

1 answer

3

Short answer

As far as data types are concerned, in JavaScript there are two basic classes, primitives and objects, so not everything is an object in JavaScript , but be careful, this could convert the primitives into an object temporarily.

Demonstration

Explanation

With code

instanseof is an operator to test if the first operand, an object, is an instance of the second, a constructor. Follow the whole chain of prototypes, if you find it returns true , if you can not find it returns false .

console.log(1 instanceof Object); //Devuelve false
console.log(Object(1) instanceof Object); //Devuelve true

Based on the documentation

The following is a statement from the glossary of Mozilla Developer Network, which in a very synthetic way clarifies that not everything in JavaScript is an object , my emphasis.

From Primitive

  

A primitive (primitive value, primitive data type) is a data that is not an object and has no methods. In JavaScript there are 6 types of primitive data: string, number, boolean, null, undefined, symbol (new in ECMAScript 2015).

From Grammar and Types

  

Boolean Literals

     

Boolean type literals have 2 possible values: true and false.

     

Do NOT confuse the Boolean primitive values true and false with the true and false values of the Boolean Object. The Boolean object is a container around the Boolean Primitive data type. For more information check Boolean .


  

Literal String

     

A literal string corresponds to zero or more characters, enclosed in double quotes (") or simple ('.) A string must be delimited by quotes of the same type, this means that they are always double or single in each of the cases The following are examples of String literals:

     
  • "foo"
  •   
  • 'bar'
  •   
  • "1234"
  •   
  • "One line \ n another line"
  •   
  • "Jhon's cat"
  •   

You can use any of the methods of the String object in a string-JavaScript literal automatically convert the string literal to a String object temporarily, call the method, and finally destroy the temporary object of type String. You can also use the String.length property with a literal string:

Below what I think are the sections of ECMAScript 2016 (Version 7) that clarify this

  

§4.3.2 primitive value

     

member of one of the types Undefined, Null, Boolean, Number, Symbol,   or String as defined in clause 6

     

NOTE
  A primitive value is a datum that is directly represented at the   lowest level of the language implementation.

     

§4.3.3 object

     

member of the type Object

     

NOTE
  An object is a collection of properties and has a single   prototype object. The prototype may be the null value.

Note that an object is not at the lowest level of language implementation, contrary to what happens with primitives.

The full explanation of the data types and values in ECMAScript 2016 (version 7) is in §6 ECMAScript Data Types and Values

On the differences between primitives and primitive objects
answered by 10.07.2017 / 02:54
source