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
Numberis an object type that wraps a primitivenumber. By explicitly creating aNumberobject, you can handle numbers like anumber, but it is usually recommended to use the primitivenumbertype.
typeof numObjectis"object", buttypeof 42is"number".
- Be aware that using the
newoperator to generate a new wrapper object yields a different result from callingNumberas a function. When called as a function, a primitivenumberis 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 aStringobject, you can access string properties and methods, but usually a primitivestringis used.
typeof strObjectis"object", buttypeof "Hello"is"string".
- Like
Number, callingStringas 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 aBooleanobject, but usually a primitivebooleantype is used.
typeof boolObjectis"object", buttypeof trueis"boolean".
- Similar to
Number, callingBooleanas 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
Objectto create a wrapper object forSymbol, but generally, you use theSymbolprimitive 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, andsymbolthat do not have direct methods. - Wrapper Objects: Objects like
Number,String, andBooleanthat 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.