Joshua Tree National Park is close enough to San Bernardino to tempt visitors into a day trip, but I would think carefully before treating it as a quick side excursion. The drive is only the beginning; the park itself is large and deserves time.
When it makes sense
If San Bernardino is your base for several nights and Joshua Tree is high on your list, an early start can produce an excellent day. The transition from the Inland Empire to high desert and then the park’s strange rock formations and Joshua trees is memorable.
Bring water, fuel up before remote areas and download maps or information in advance. Desert conditions should never be treated casually.
When I would not do it
If you only have two days in San Bernardino, choose the nearby mountains or Route 66 instead. Spending hours driving just to rush through Joshua Tree is not a good trade. Extreme summer heat also changes what is sensible.
Consider staying overnight
If your road trip continues east, Joshua Tree works better as the next destination rather than an out-and-back excursion. Yes, it is possible as a day trip. Whether it is sensible depends on how much time you can give it.