Back to homepage

TIL of JavaScript

Published on 13/05/2021
I think it's fun to share haha.

FormData object is NOT console.log-able

// Create a test FormData object
var formData = new FormData();
formData.append('key1', 'value1');
formData.append('key2', 'value2');

// Display the key/value pairs
for (var pair of formData.entries()) {
    console.log(pair[0]+ ', ' + pair[1]); 
}

I spent half hour to debug a FormData object by console.log(theFormDataObject) but I couldn't get the data I appended to it. Later on I found it's not console.log-able haha. A way to output form data is in the code block above.

JavaScript doesn't have Associative Array

Nop, there's no such thing. I'm a PHP developer so I ran into this Gotcha when I output data in PHP with json_encode() . The JavaScript I'll get is actually an object. E.g.

// PHP

json_decode([
  'name' => 'Lisa'
]);

// JavaScript
{
  name: 'Lisa'
}

I won't be able to access Lisa by theObject['name'] . Because JS doens't have Associative Array.

Maybe Map is close to Associative Array. But the way to get and set value of a Map object is to use .get and .set.

Be the first
to get my latest blog
Comments