1

Is undefined an object in javascript?

ex: string is an object because string.length exists.

5
  • string and undefined are not objects Commented Dec 2, 2019 at 9:08
  • Well, you can use inspector in your browser and type typeof(undefined), and you'll get "undefined". String is also a function, and not an object. Commented Dec 2, 2019 at 9:09
  • 3
    string.length is not evidence that string is an object: javascriptweblog.wordpress.com/2010/09/27/… Commented Dec 2, 2019 at 9:09
  • 1
    @adiga — string might be. We can't see how it is defined. Commented Dec 2, 2019 at 9:10
  • @Quentin yes, yes. I was focusing on the .length argument. Commented Dec 2, 2019 at 9:13

4 Answers 4

6

undefined is a primitive:

The global undefined property represents the primitive value undefined. It is one of JavaScript's primitive types.

Strings are primitives too. When you use <referenceToString>.<someProperty>, the interpreter wraps the string in an object wrapper so that it can reference String.prototype.

Sign up to request clarification or add additional context in comments.

Comments

5

No, undefined is a primitive, the only instance of the Undefined type. Details in the specification, but the primitive types are (currently):

  • Undefined
  • Null
  • Boolean
  • Number
  • BigInt
  • String
  • Symbol

(It's likely more will be added, such as BigDecimal.)

Everything else is an object.

ex: string is an object because string.length exists.

Strings are not usually objects, they're primitives that are coerced to their object equivalent when necessary (in theory; in practice, JavaScript engines optimize away that conversion). For instance:

let s = "foo";

s is a primitive string, typeof s is "string". But when you try to access a property on it (s.length for instance, or s.toUpperCase()), the primitive string is coerced to a String object. (Again: in theory — and sometimes in practice, particularly in loose mode.) That's how it gets access to its methods, which are defined on String.prototype. You can also create a String object intentionally: new String("foo") but there's almost never any good reason to do that.

This same is true for all of the other primitive types except Undefined and Null, which have no object type equivalent (if you try to access a property on undefined or null, you get a TypeError). (This is a bit confusing for Null, because typeof null is "object" and null is used when something object-typed needs a "no value" value. But Null is a primitive type, and null is a primitive value.)

4 Comments

I missed that BigInt is already done... have to pay more attention :D
@FelixKling - :-) My money is on BigDecimal in ES2021 as well.
@adiga - Thanks. You'd be truly shocked how often I make that typo when not typing in an IDE... :-)
why does typeof null gives object, and why does Object.prototype.toString.call(undefined) gives [object Undefined]
0

4.3.2 primitive value

member of one of the types Undefined, Null, Boolean, Number, Symbol, or String

6.1 ECMAScript Language Types

An ECMAScript language type corresponds to values that are directly manipulated by an ECMAScript programmer using the ECMAScript language. The ECMAScript language types are Undefined, Null, Boolean, String, Symbol, Number, and Object. An ECMAScript language value is a value that is characterized by an ECMAScript language type.

6.1.1 The Undefined Type

The Undefined type has exactly one value, called undefined. Any variable that has not been assigned a value has the value undefined.

3 Comments

Thanks for contributing! But this just repeats what previous answers have already stated, which isn't useful. It's also out of date (missing BigInt). The up-to-date, living specification is here. It's best to link to that unless you're talking about a specific version for some reason. But again, this just repeats what previous answers have said.
Aha, I didn't know the 2020 draft was out 😦You probably know this better than me, but when will BigInt be implemented into the engines?
The draft is started immediately after the last snapshot is forked, then proposals that reach Stage 4 are added to it as part of reaching Stage 4. According to caniuse, BigInt is implemented in recent versions of V8 (Chrome, Chromium, Brave, etc., and the Chromium version of Edge currently in beta), SpiderMonkey (Firefox), and in the JavaScriptCore in the latest Safari Technical Preview.
-1

undefined is a property of the global object; i.e., it is a variable in global scope. The initial value of undefined is the primitive value undefined. A variable that has not been assigned a value is of type undefined. Eg. In the following code, the variable x is not initialized, and the if statement evaluates to true.

var x;
if (x === undefined) {
   // these statements execute
}
else {
   // these statements do not execute
}

3 Comments

undefined is a property of the global object; i.e., it is a variable in global scope. The initial value of undefined is the primitive value undefined.
Then you should edit your answer and say 'global object' instead of the object , to make it clearer
@ShubhamVernekar "undefined is a property of the global object" Yes, that's true, but it's not really an answer to the question. "i.e., it is a variable in global scope" You want to be a bit careful there, not all globals are properties of the global object (not since ES2015). :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.