How do I check if a variable exists?

  • 0
    What is the shortest way to do so that it does not give an error if objects do not have a certain property, for example $ {e.breeds [0] .name}
    function createPics() {
    		getCats();
    		let containerCats = '';
    
    		newCats.forEach(e=>{
    			let htmlCats = `
    			<div class="cat__item">
    				<div class="cat__img"><img src="${e.url}" alt="${e.breeds[0].name}"></div>
    				<div class="cat__name">${e.breeds[0].name}</div>
    				<div class="cat__temperament">${e.breeds[0].temperament}</div>
    				<div class="cat__live">${e.breeds[0].life_span}</div>
    				<div class="cat__origin">${e.breeds[0].origin}</div>
    				<div class="cat__description">${e.breeds[0].description}</div>
    				<div class="cat__wikipedia_url">${e.breeds[0].wikipedia_url}</div>
    			</div>
    			`;
    			containerCats +=htmlCats;
    
    		})
    		generalDiv.innerHTML = containerCats;
    	}
    JavaScript Ronan Horton, Sep 9, 2019

  • 2 Answers
  • 0

  • 0

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!