|Value||Converted to String||Converted to Number||Converted to Boolean||Converted to Object|
Changing from primitive to object type seems quite simple, it’s just calling of certain functions, like “Computer Science Hub” string could be converted to an object by just saying new String(“Computer Science Hub”).
- String primitive to String object => new String()
- Number primitive to Number object => new Number()
- Boolean primitive to Boolean object => new Boolean()
Converting an object to a primitive type is quite tedious/tricky thing as object may be created from string/number/boolean, but then reverse engineering that process and coming up with exactly what’s object was created from is very difficult.
- Boolean(“Computer Science Hub”) // returns true
- Boolean(“1002”) // returns true as well
- Boolean(“”) //returns false
|An array having just one element may also convert to a number|
|Empty string converts to number 0|
|Array having single element always converts to string|
|Array containing multiple numbers converts to string of numbers|
Type Conversions before doing Arithmetic
- “0” == 0 //Would be same
- “Computer Science Hub” == true //this would also be same
- Reference Equality
- Manual Comparison
- Shallow Equality
- Deep Equality
But rather it’s always a best practice to have at least some Explicit type conversions, if it seems weird or you just want to keep a track of types of entities/variables in code.
- Number(“10304”) // converts string 10304 to number 10304
- String(true) // converts string true to Boolean true
- Object(1002) // converts object 1002 to number 1002
Moreover if something is not clearer to you from this article and you want to ask a question, then please comment down below and I would try to reply as soon as possible. Happy Coding!!!!