#how to ignore the undefined value in js

1 messages · Page 1 of 1 (latest)

balmy galleon
#

I have made a book array with nested object showing the different books information like title,author etc.
For example :
Const books = [
{
Title: ABC
OnlineRead: true
}
{OnlineRead : false
... Etc}
]

One of the books variable is onlineRead
But in one of the book this variable is not declared
And I was practicing short circuits, I have to log the books with true onlineRead
So my code is
For(let I =0;I <= books.length; I++) {
Return books[i].onlineRead && console.log( book is online ) }
But I forgot how to ignore undefined

#

Sorry I am new to coding so my sentence can be confusing

finite hornet
balmy galleon
finite hornet
balmy galleon
#

My code is

for(let I = 0,i <= books.length;I++) {
books[i].online && console.log(`${books[i].title} is available online`) 
}
balmy galleon
finite hornet
#
const books = [
  {
    title: "Hello World",
    online: true
  },
  {
    title: "Hello World2",
    online: true
  },
]

for(let i = 0; i < books.length; i++) {
  if (books[i].online) console.log(`${books[i].title} is online!`)
}

i made an example that works correctly

finite hornet
#

then if online is undefined or false it wil not console log it