JavaScript
Object property to string

by Benny Neugebauer

const error = {
  code: 'ENOENT'
}

const result = ['ENOENT', 'ENOTFOUND'].includes(error.code.toString())

const error = {
  code: 'ENOENT'
}

const result = ['ENOENT', 'ENOTFOUND'].includes(String(error.code))

If prefer String(error.code) because in case code is undefined it won't fail whereas toString() fails miserably in that case.

Benny Neugebauer

Sign in to comment!