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

javascript array of objects
array javascript
javascript object
javascript array methods
javascript map
object to string javascript
javascript multidimensional array
javascript array push
 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

  1. If at least one operand is an object, it is converted to a primitive value (string, number or boolean);
  2. After conversion, if at least one operand is string type, the second operand is converted to and the concatenation is executed;
  3. 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

JSON.stringify(), How do you turn an object into a string in JavaScript? JavaScript is a loosely-typed language and will convert from one type to another when necessary. ^. All objects in JavaScript have a toString method available for converting their values to strings. It may be the toString method inherited from the generic Object, or it may be specific to the particular type of object.

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"

How to convert an Object {} to an Array [] of key-value pairs in , Sure, to convert an object into a string, you either have to use your own method, a JS engine, the output for console.log('Item: ', o); is still Item: [object Object] . but JavaScript // ignores them. function convertToText(obj) { //create an array var convertedString=JSON.stringify(obj) console.log("literal object is",obj ,typeof  Typescript Object Array. Typescript is superset of javascript with compile type checking. In Application development, We used to get the use cases where data retrieved from REST API/Database in the form of Array/Object, so need to convert this to Object/Array. Every developer used to get this user cases during development.

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])

Converting an object to a string, The JavaScript Array class is a global object that is used in the construction Neither the length of a JavaScript array nor the types of its elements are fixed. Since an array's length can change at any time, and data can be stored at The 2 in years[2] is coerced into a string by the JavaScript engine through  While developing a JavaScript application, you will sometimes want to serialize your data into a plain string. This can be useful for things like: Storing object data in a database Outputting object data to the console for debugging Sending object data over AJAX or to an API The simple syntax for converting an object to […]

Array, arrayLike: An array-like or iterable object to convert to an array. like typed arrays, since the intermediate array would necessarily have values truncated to fit into the appropriate type. charCodeAt(0), 1).map(x => String. The data type of a date is object. The data type of null is object. The data type of an undefined variable is undefined *. The data type of a variable that has not been assigned a value is also undefined *. You cannot use typeof to determine if a JavaScript object is an array (or a date).

Array.from(), Fortunately, JavaScript provides a data type specifically for storing sequences of You give it an object, and it returns an array of strings—the object's property names. You can change their properties, causing a single object value to have So, Jacques starts up his JavaScript interpreter and sets up the environment he​  An array in JavaScript is a type of global object used to store data. Arrays can store multiple values in a single variable, which can condense and organize our code. JavaScript provides many built-in methods to work with arrays, including mutator, accessor, and iteration methods.

Data Structures: Objects and Arrays :: Eloquent JavaScript, On the positive side, though, array literals can replace the need for temporary variables, especially when sending values to a function. In this example, the JavaScript engine makes the array-to-string conversion for both the array literal and the array object contained as an element within the array literal. See the article “Determining with absolute accuracy whether or not a JavaScript object is an array” for more details. Given a TypedArray instance, false is always returned. Examples

Comments
  • I don't know why it does it but I do know that you can use empArr.push(arr[i]) to have it stay an array
  • Because you don't use += to push to arrays, that is a string append operation.
  • Possible duplicate of Why is [1,2] + [3,4] = "1,23,4" in JavaScript?
  • @BenFortune I agree we don't use += to push arrays,by mistake i did this and saw this result so wanted to know how JS engine is behaving here.
  • As per the link @Jeto posted, the + operator is not defined for arrays, Javascript will convert arrays into strings and concatenate those