How to conditionally add a member to a JavaScript object?

This article describes how to add a member to a JavaScript object only if a condition is met. We demostrate it in pure JS and also in JQuery.

Pure JavaScript version

In pure javascript, the most compact solution I could come up with is this:

var myObject = {};

if(true) myObject.member1 = 10;
if(false) myObject.member2 = 20;
if(true) myObject.member3 = 30;

In this example only the first and third member will be added. It is quite okay, but looks a bit verbose, especially because all of the assignments are separate statements, and you have to write down the name of the object multiple times. It would be good to have a solution that can handle conditional adding when declaring the object. Let’s look at jQuery…

jQuery version

As you might expect, in jQuery it is a bit more compact. Let’s see the solution:

var myObject = $.extend({}, {
    member1: true  ? 10 : undefined,
    member2: false ? 20 : undefined,
    member3: true  ? 30 : undefined,

As you can see, it looks better, and you can assign the members when you are creating the object. However, you have to write undefined in all places, but even considering that, it is a more compact solution.

This code uses the extend function that can add the members of one JavaScript object to another JavaScript object. As you can see we set all the attributes of the second object, but only the ones that have a value that is not undefined will be set.

Change the text of a link via CSS

A typical example of a link that changes it’s text is when you would like to create section on your website and need a link that says Collapse when the section is open and says Expand when the section is closed.

Create a simple closable section

First, let’s create a simple link that will open and close our section with the help of a little JavaScript. The HTML part:

<a class="toggle-button" href="#">Collapse/Expand</a>

<section class="expandable">
    This is an expandable section.

The JavaScript part:

$(function() {
    $('.toggle-button').on('click', function() {

Clicking the link can open and close the section. So our program is working fine, but we could improve it a little bit by changing the text of the link depending on the status of our section.

Add text changing functionality via CSS

Let’s add a wrapper around our HTML elements and change the classnames:

<div class="expandable-container">
    <a class="toggle-button" href="#" data-collapsed="Expand" data-expanded="Collapse"></a>
    <section class="expandable-content">
        This is an expandable section.

Our JavaScript will also need to be changed, because we would like to add the expanded class to the parent of the content that we would like to show/hide.

$(function() {
    $('.toggle-button').on('click', function() {

Now comes the most important part. As you can see in our HTML we provided the Expand and Collapse texts is data attributes. We will use CSS to read these attributes and set them as the link’s text.

.expandable-container .toggle-button:after {
    content: attr(data-collapsed);
.expandable-container.expanded .toggle-button:after {
    content: attr(data-expanded);

.expandable-container .expandable-content {
    display: none;

.expandable-container.expanded .expandable-content {
    display: block;

The text of the link is set using the :after psedo attribute. The content inserted this way should behave as it would be a normal child of the element, but some browsers have issues interpreting them. In an other article I discussed a possible problem that could come up on certain browsers: Link with pseudo attribute is not clickable on mobile browser (Samsung Galaxy S3)

Array has an extra undefined element in IE 8

I’ve run into this problem when I was writing a new piece of JavaScript code that went through an array and did something on all it’s elements. We needed to support the latest FF, Chrome and IE 8+. In almost all browsers it was working fine. However, in IE 8 my array had an extra undefined element at the end. My array definition looked something like this:

var arr = [1, 2, 3,]

The problem is the trailing comma at the end. IE 8 thinks that the comma means there is an extra element in the array and because it is not specified it becomes undefined.  Then if you go through all the element and try to call some function on them, you will get an error stating that the desired function is not available on that element. To fix this, just remove the trailing comma.

var arr = [1, 2, 3]