Stringify global objects – debugging helpers
Tags: javascript, debugging
Published: 2021-12-29 21:57:16

### <u>[object Window]</u>

```javascript
Object.prototype.toString.call(this) // '[object Window]'
Object.prototype.toString.call(window) // '[object Window]'
this === window // true
```

---

### <u>[object Object]</u>

```javascript
function stringify (x) {
    console.log(Object.prototype.toString.call(x));
}
console.log(stringify({})); // console output: [object Object]
// typeof {} // 'object'
```

> **NOTE** : `[object Object]` is most common one . Sometimes it happens then you e.g.: 
```javascript
console.log(`Begin ${object_variable} end.`) 
```
: if variable of `object_variable` returns object rather than primitive you will definitely end up into [object Object], solution would be console.log() it outside the template literals e.g.:
```javascript
console.log(`Begin`, object_variable, `end.`)
```
: unless `object_variable.some_property` whereas `some_property` is some primitive such as Number or String literal then you could stick within template literals as in a first place shown above .   

---

### <u>[object Array]</u>

```javascript
typeof [] /* 'object' : technically it's true as everything in JS is an object under the hood , although it limits debugging , a solution to this is : */
function stringify (x) {
    console.log(Object.prototype.toString.call(x));
}
console.log(stringify([])); // console output: [object Array]
// alternatively do so :
Object.prototype.toString.call([]) === '[object Array]' // true
// or simply  :
Array.isArray([]) // true 
```

> **NOTE** : we use **Array.prototype** quite often , sometimes we need to check type i.e. double check if it's not an object an we can apply Array.prototype otherwise if e.g. `.push()` applied upon **Object.prototype** would end up in an error message of ".push() is not a function" – common sense – you cannot expect otherwise as Object.prototype is not ant Array i.e. it has no idea what a heck is `.push()` & what it is doing in a wrong world of Object's ..!

---

### <u>[object String]</u>

```javascript
// Hereby string presented as object rather than primitive !
Object.prototype.toString.call("" || '' || ``); // [object String]
// e.g.:
Object.prototype.toString.call("" || '' || ``) !== Object.prototype.toString.call(RegExp); // true
// although be careful :
String.prototype.toString.call("food"); // "food"
Object.prototype.toString.call("food"); // [object String]
```

---

I guess least used option would be a typeof 'function' , i.e.:

### <u>[object Function]</u>

```javascript
// in function declaration form :..
Object.prototype.toString.call(function(){}); // '[object Function]'
typeof function(){} // 'function'
// in function factory form :..
typeof Function // 'function' 
Object.prototype.toString.call(Function); // '[object Function]'
// although be careful as some X.prototype may still be returned as Function if CCCapitalized e.g.:
Object.prototype.toString.call(Object || Array || String || RegExp); '[object Function]'
```

---

**To conclude** : in most of programming langs the most important  is to know what type is returned (debugging) or expected to be returned (testing) – then you know the type i.e. the X.prototype you are dealing with – worst is in the past . Now jump in your IDE & do some coding as well as little of debugging cause it's fun !