Can we store the JavaScript object as a variable?

Question

In the context of this exercise, the example calls the .css() function

$('.example-class').css({
  color: '#FFFFFF',
  backgroundColor: '#000000',
  fontSize: '20px'
})

with the JavaScript object passed in. Can we store the object as a variable instead of passing this in to the .css() method?

Answer

Yes, when calling the .css() method, you can pass in an object of key/value pairs which is stored in a variable instead. This can help to make the code more organized and easier to read.

For example, you might do the following,

var styles = {
  color: '#FFFFFF',
  backgroundColor: '#000000',
  fontSize: '20px'
}

$('.example-class').css(styles);
10 Likes