Wrapper Objects in JavaScript
This article explains about wrapper objects in JavaScript.
YouTube Video
Wrapper Objects
JavaScript has primitive types and objects to handle them (wrapper objects).
Primitive Types
Primitive types represent a single value and are simple and lightweight. These are immutable (unchangeable) and do not directly have object methods or properties, but they can be temporarily converted into wrapper objects for operations.
string
and number
are examples of primitive types. On the other hand, wrapper objects start with a capital letter, like String
and Number
.
Wrapper Objects
To manipulate primitive types, JavaScript temporarily generates wrapper objects (Object
wrappers). Wrapper objects contain methods and properties for the primitive types.
Number
1let numObject = new Number(42); // Wrapper object
2
3console.log(typeof numObject); // object
4console.log(numObject.valueOf()); // 42
5
6let numValue = Number(42); // Return primitive value
7
8console.log(typeof numValue); // number
9console.log(numValue); // 42
Number
is an object type that wraps a primitivenumber
. By explicitly creating aNumber
object, you can handle numbers like anumber
, but it is usually recommended to use the primitivenumber
type.
typeof numObject
is"object"
, buttypeof 42
is"number"
.
- Be aware that using the
new
operator to generate a new wrapper object yields a different result from callingNumber
as a function. When called as a function, a primitivenumber
is returned.
String
1let strObject = new String("Hello"); // Wrapper object
2
3console.log(typeof strObject); // object
4console.log(strObject.valueOf()); // Hello
- It is an object type that wraps a primitive
string
. By using aString
object, you can access string properties and methods, but usually a primitivestring
is used.
typeof strObject
is"object"
, buttypeof "Hello"
is"string"
.
- Like
Number
, callingString
as a function returns a primitivestring
.
Boolean
1let boolObject = new Boolean(true); // Wrapper object
2
3console.log(typeof boolObject); // object
4console.log(boolObject.valueOf()); // true
- It is an object type that wraps a primitive
boolean
. You can also explicitly create aBoolean
object, but usually a primitiveboolean
type is used.
typeof boolObject
is"object"
, buttypeof true
is"boolean"
.
- Similar to
Number
, callingBoolean
as a function returns a primitiveboolean
.
BigInt
1let bigIntObject = Object(9007199254740991n); // Wrapper object
2
3console.log(typeof bigIntObject); // object
4console.log(bigIntObject.valueOf()); // 9007199254740991n
5
6let bigIntValue = BigInt(9007199254740991); // Return primitive BigInt
7
8console.log(typeof bigIntValue); // bigint
9console.log(bigIntValue); // 9007199254740991n
10
11// Error
12let bigIntObject2 = new BigInt(9007199254740991n);
Object
を使ってBigInt
のラッパーオブジェクトを生成できますが、通常はプリミティブ型のbigint
を直接使うのが一般的です。
typeof bigIntObject
は"object"
であるのに対し、typeof 9007199254740991n
は"bigint"
です。
BigInt
を関数として呼び出すとプリミティブなbigint
を返しますが、new
演算子をつけてBigInt
のラッパーオブジェクトを生成することはできません。
Symbol
1let symObject = Object(Symbol("id")); // Wrapper object for Symbol
2
3console.log(typeof symObject); // object
4console.log(symObject.valueOf()); //
- You can use
Object
to create a wrapper object forSymbol
, but generally, you use theSymbol
primitive type directly.
Differences between Primitive Types and Wrapper Objects
- Primitive types are lightweight and memory efficient, but they do not have direct properties or methods. However, in JavaScript, even with primitive types, temporary wrapper objects are automatically created when needed to provide methods and properties.
1let text = "Hello";
2
3// A temporary String object is created,
4// allowing access to the length property
5console.log(text.length);
- Wrapper objects are explicitly generated objects that can impact memory and performance, so using primitive types is generally recommended.
Conclusion
- Primitive Types: Lightweight types such as
number
,string
,boolean
,null
,undefined
, andsymbol
that do not have direct methods. - Wrapper Objects: Objects like
Number
,String
, andBoolean
that are temporarily converted from primitive types.
You can follow along with the above article using Visual Studio Code on our YouTube channel. Please also check out the YouTube channel.