What is difference between "string" and string in javascript - 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.

Related

How is JavaScript Engine changing the type of Array Object to String

let arr = [1,2,3];
let empArr =[];
for(var i =0; i< arr.length;i++){
console.log('First',typeof empArr);
empArr+=arr[i];
console.log('Second',typeof empArr)
}
The above code gives this output
First object
Second string
First string
Second string
First string
Second string
Could anyone explain how in first iteration type was Array Object then after that it became string.How Javascript Engine works here?
If we run typeof empArr, we will see empArr an object. No matter if we declare it as an array, internally, it is an object. Further, typeof arr[i] shows arr[i] is a number. Therefore, empArr+=arr[i] means we are trying to add an object and a number. Since we are trying to add two different types, it can happen with the help of coercion, implicitly. Coercion means, converting a value of one type to another. JavaScript performs implicit coercion as per the following rules:
operand + operand = result
If at least one operand is an object, it is converted to a primitive
value (string, number or boolean);
After conversion, if at least one operand is string type, the second operand is converted to and the concatenation is executed;
In other case both operands converted to numbers and arithmetic addition is executed.
Note that the primitive value of an array or object is a string.
In our case, empArr is of type object and by rule 1, it is coerced as a string. Now by rule 2, the arr[i] which is a number, is coerced to a string as well and get assigned to empArr.
For more details:
JavaScript addition operator in details
JavaScript type coercion
According to javascript,
typeof [] is "object" ,i.e., every array is actually an object.
if you append anything to a string it will become a string
"1"+1 will equal "11"
It's because of auto type conversion.
The += is a not an array operator, and as the second operand is string - the 1st is converted to string.
Use empArr.push(arr[i])

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

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".

Why does 'string' + undefined => 'stringundefined'?

The question is pretty straight forward. In Javascript, why does:
'string' + undefined
//=> 'stringundefined'
I would have expected just string to be returned, or at least an error stating you can't convert undefined into a string data type.
Since you are performing a string concatenation, forced type conversion occurs. undefined is being converted to its string value and added to the string
'string' + undefined
Step 1: 'string'
Step 2: undefined -> 'undefined'
Step 3: 'string' + 'undefined'
When you concatenate strings each item gets cast to a string, and undefined becomes "undefined".
String(undefined); // "undefined"
interesting question.
Because ECMAScript use internal ToString operation to convert primitives to string.
undefined=> "undefined"
null=>"null"
boolean=>either "true" or "false"
number>the number as a string e.g. "1.765"
http://www.2ality.com/2012/03/converting-to-string.html

Why is typeof's result different than the evaluated result of the expression passed in?

If two Objects added together equal NaN(not a number), which is technically of type number, then why does getting the type of two Objects added together result in "string"?
I will express this via the REPL:
> {} + {}
> NaN
ok. two objects added together creates NaN
> typeof(NaN)
> "number"
ok. we know that NaN's type is "number"
> typeof({} + {})
> "string"
wait. shouldn't this have been "number" also?
I'm aware that javascript has a less then desireable type system, but I'm confused as to what's happening here. Is the type being converted from number to string for some reason? Is number even a part of the type converting that goes on here? Or am I just using typeof wrong?
{} + {} is an empty block ({}) followed by a type conversion from object to number (+{}). It basically reads as
{} // empty block (a statement)
; // empty statement (just for clarity)
+{}; // expression statement (unary plus, object literal -> conversion to number)
However if you use typeof ({} + {}), then {} + {} will be evaluated as expression in which case both {} can only be object literals and the + is the concatenation operator.
You can also just use the grouping operator to force the construct to be evaluated as expression:
({} + {}) // expression statement (string concatenation with two objects)
See also Why {} + {} is NaN only on the client side? Why not in Node.js? and other questions related to [javascript] "{} + {}".

Is it possible to check for a possible conversion in JavaScript?

In JavaScript, variables are loosely typed, so the number 5 and the string "5" may both be treated as a number by several operators. However, is there a generic way to find out JavaScripts conversion abilites in at tunrime, or is it just the overloading of operators for several types that make the loose typing possible?
For example, given a variable a and a string containing a type name type_canditate, is there any way to ask JavaScript, if a may convert to type_candidate in a feasable manner, in contrast to the hard typing operators like instanceof? For example, "5" instanceof Number evaluates false, while Math.sin("5") is perfectly feasable. For numbers, one can obviuosly check if parseFloat(some_number) evaluates to NaN, but this is a special case for numbers.
So, is there any generic way of naming types, and check if some variable may convert to a given type in a useful manner?
There are three primitive data types in JavaScript: string, number and boolean.
Anything can be converted to a string or boolean:
All objects convert to true (except null, which becomes false - I only mention it here because typeof null gives object)
All objects have a built-in toString method which is called when converting to a string.
Converting a number to a string is done by giving the string representation of the number (ie. 5 becomes "5")
Numbers convert to boolean true, unless it's 0 which becomes false.
Converting to a number is a little trickier, but technically possible. If it can find a valid number, then it becomes that number. Otherwise, it becomes NaN.
So basically... any type can become any other type through casting in this way. The only time you have anything resembling an "error condition" is NaN.

Categories