JSFiddle - React, Tailwind, and code Playground
HTML
<div id="bart"></div>
JavaScript
//Goal: do most of the logic in a single line using just the OR
//operator and a ternary
// bar, either undefined or a cached selection of $('#bar')
var bar = $('#bar');
// if bar isn't undefined/falsey, use bar, otherwise query the
// DOM for the div 'bar' so we can cache it in 'foo' if
// bar has a length greater than 1
// if bar doesn't exist in the global scope and isn't currently
// in the DOM, we'll dynamically create a div and set it's ID to
// 'bar' instead.
var foo = bar = bar && bar.length && bar ||
(bar = $('#bar')).length ? bar :
$('<div />', { id: 'bar' });
console.log(foo, bar);