In Javascript is an String literal an object? [duplicate] - javascript

This question already has answers here:
What is the difference between string primitives and String objects in JavaScript?
(12 answers)
Closed 7 years ago.
I'm reading about the difference between String literal and String objects. See
What is the difference between string literals and String objects in JavaScript?
But I'm a bit confuse because there it's explained that you can use methods of the String Objects but technically is a String literal an String Object? I'm not asking if we can use the same methods, only if a String literal is an object. Thanks!

The term "string literal" refers to a syntactic convention for representing string values directly in code.
The code
"Hello Everyone"
is a string literal for a string with 14 characters.
The value represented by a string literal is a string primitive. It is not an object. That's why if you use:
typeof "Hello Everyone"
this will return the value "string", not "object".
JavaScript allows boxing of any string primitive to promote them to string objects under certain circumstances. Attempting to call a method on a string value is one of these circumstances. So if you call:
"Hello Everyone".toUpperCase()
the value represented by this literal will be boxed into a string Object, and the method will be called on that object.

You can check the type of a Javascript variable with the typeof operator. typeof "Hello World" and typeof String("Hello World") both return the type "string".
Also, a strict-equal check "Hello" === String("Hello") returns true, which means that they are not just value-equal but type-equal.
However, typeof new String("Hello World") returns "object".

Related

Javascript not replacing single character [duplicate]

This question already has answers here:
Are JavaScript strings immutable? Do I need a "string builder" in JavaScript?
(10 answers)
Closed 2 years ago.
This code does not work
var x ='hello';
x[2] ='a';
Why doesn't it work?
I am not using the replace() function
In javascript the strings are immutable, you cannot change the string using index.
The string manipulation methods such as trim, slice return new strings.
It doesn't work because of legacy behavior of promoting string values to String object wrappers with the value of the primitive string value.
So for example
"xyz".charAt(0)
is at least notionally evaluated as
(new String("xyz")).charAt(0)
because charAt is a prototype method of String objects and can be applied to a primitive string value in this manner.
The down side is that the promoted String object is never stored anywhere and, even if modified in JavaScript is immediately discarded. This is what is happening in the code posted:
var x ='hello';
x[2] ='a';
is treated as
new String(x)[2] = 'a'
followed by discarding the String object which, incidentally, has a property named 2 with value 'a'.
The general solution to this is to run JavaScript in strict mode, so that any attempt to assign a property to a primitive value which has been promoted to an object wrapper generates an error:
"use strict"
var x ='hello';
x[2] ='a';

What is difference between "string" and string in javascript

I was reading you dont know js (Up and going) and i stumbled across this piece of line
The return value from the typeof operator is always one of six (seven
as of ES6!) string values. That is, typeof "abc" returns "string", not
string.
I am unable to understand difference between "string" and string.
Whenever i check any typeof for a string value,
the value is "string".
Can I get help in understanding this
The typeof operator always returns a string type. In this instance, it just so happens that the type you're checking is a string. How do you represent the type string as a string itself? Wrap it in quotes.

Is there a difference between String(x) and ''

Is there a difference? Will string 2 inherit different object prototypes?
var s1 = 1234 + '';
var s2 = String(1234);
//s1.someNewFunc(); error?
//s2.someNewFunc();
Thanks
var s1 = 1234 + '';
Creates a string literal. This is a javascript language primitive.
var s2 = String(1234);
The String() function also returns a primitive string literal. s2 will have the same members as s1 because they are both the same type.
However
var s3 = new String("1234");
Will create an object of type String rather than a primitive string literal. This does have different members and is of type object.
Same thing!
var s1 = 1234 + '';
var s2 = String(1234);
typeof s1 //string
typeof s2 //string
Both will behave the same way.
Also, there is a nice explanation about string primitives vs. objects here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String
Distinction between string primitives and String objects
[...] String literals (denoted by double or single quotes) and strings returned from String calls in a non-constructor context (i.e., without using the new keyword) are primitive strings. JavaScript automatically converts primitives to String objects, so that it's possible to use String object methods for primitive strings. [...]
Javascript allows you to treat primitive values as though they were objects. It does so by doing an on-the-fly coercion of the primitive to an object. That's why, even though primitives have no properties, something like this is perfectly fine:
"abcde".substr(1,3); //bcd
true.valueOf(); //true

Strings in javascript

I have recently seen some references that explain Strings in Javascript as a primitive type.I know that a primitive is a data type that is composed of no other data types and can not be broken down any further.But the problem is I have also read strings are objects.How it can be both ?Please clarify me about the confusion.
You can read about that exact topic on MDN:
Note that JavaScript distinguishes between String objects and
primitive string values. (The same is true of booleans and numbers.)
String literals (denoted by double or single quotes) and strings
returned from String calls in a non-constructor context (i.e., without
using the new keyword) are primitive strings. JavaScript automatically
converts primitives to String objects, so that it's possible to use
String object methods for primitive strings. In contexts where a
method is to be invoked on a primitive string or a property lookup
occurs, JavaScript will automatically wrap the string primitive and
call the method or perform the property lookup.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String

JavaScript parentheses converts primitive type to object

If numbers are primitive types, why I can do:
> (12345).toString()
"12345"
Is the parenthesis converting the primitive type to a Number?
No, the parentheses are just letting the parser understand that the . is not a decimal point.
12345 .toString() will also work.
Primitive numbers implicitly converted to Numbers whenever you access their properties, but the objects are temporary and immediately lost. For example:
var foo = 5;
foo.bar = "something";
console.log(foo.bar); // undefined
Same goes for strings and booleans.
Actually, 1 .toString() works as well.
>>> typeof(Number(1)) === typeof(1)
true
>>> var a=1; a.toString()
"1"
It's the parser: 1.x expects x to be a digit.
>>> 1.toString()
SyntaxError: identifier starts immediately after numeric literal
[Break On This Error]
You can find further explanation here
If primitives have no properties, why does "abc".length return a
value?
Because JavaScript will readily coerce between primitives and objects. In this case the string value is coerced to a string object
in order to access the property length. The string object is only used
for a fraction of second after which it is sacrificed to the Gods of
garbage collection – but in the spirit of the TV discovery shows, we
will trap the elusive creature and preserve it for further analysis…

Categories