Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conversion of String Object to String Primitive in Javascript

Tags:

javascript

A silly question I guess, how can we convert a String object in javascript to a String primitive ?
Problem is I am having a map in which key is a String literal and it is not giving any result if I am passing a String object to it. Any way i can covert that string object to primitive to get the results from map ?

like image 720
snow_leopard Avatar asked Sep 26 '14 14:09

snow_leopard


1 Answers

You can use the valueOf method to extract the primitive value from a wrapper object:

var sObj = new String("foo");
var sPrim = sObj.valueOf();

Wrapper objects (String, Boolean, Number) in JavaScript have a [[PrimitiveValue]] internal property, which holds the primitive value represented by the wrapper object:

[[PrimitiveValue]]: Internal state information associated with this object. Of the standard built-in ECMAScript objects, only Boolean, Date, Number, and String objects implement [[PrimitiveValue]].

This primitive value is accessible via valueOf.

like image 63
apsillers Avatar answered Oct 21 '22 07:10

apsillers