Back to Blog

How to check if an object property is undefined

Within your chatbot you can work with several objects that contain properties. Based on these properties you can adjust your flow. Consider the following example:

You customer service bot is available as a Web Widget and is active in two places:

  1. Behind a login
  2. On your home homepage (no login required)

When the chat between end-user and bot requires a handover or takeover, you might want to adjust your flow based on the information that you have from your end-user. In the case where the hand-over is behind a login, you will be able to get back to the end user as name and email is visible for you. When the user enters the handover from the homepage, his or her profile is undefined and you might want to ask for his or her first name and e-mail address.

The typeof operator

You can use the typeof operator in JavaScript to check the type of an object property. The typeof operator will return a string that contains one of the following:

Check if Undefined

In the following example we will adjust our flow based on the user’s email address. If the email address of the user exists (e.g. “[email protected]”), the typeof operator will return ‘string’. If the user is Anonymous it will return ‘undefined’.

If the returned value is a string we can assume that the email address is known. Therefore we trigger an event “Profile-Known”. If the returned value is not a string, it can be an object, number or undefined. In that case we trigger an event “Profile-Unknown”. Make sure to create events that match those names in your design.

async payload => {
  // check if a string will be returned
  if(typeof [][0] === 'string') {
    // Trigger an event

  } else {
    // Trigger other event
Get StartedContact us