No they aren't the same, but... technically any of them CAN be swapped, but past that statement there are complications, like the control systems, fuel delivery, transfer cases... lots of stuff!
As a general rule though; 84-86.5 are computer controlled carbuerated, 86.5~91? are TBI, 91(92)~97 or something are MPI, and past that up until 2006 were sequential multi port (correct me if I'm wrong on these, just off the top of my head).
ALL of them use the same block, with XJ and MJ blocks shaved for a higher compression (from 9.1:1 to 9.2:1) and more power (only something like 4hp or so), with it downtuned in the Wrangler until the TJ came out?
Intake Manifold designs change throughout the years (obviously, as far as from Carb to TBI to MPI goes, but also I believe the MPI intake changed after 2000? to restore some of the power that was robbed due to higher emission standards)
Some years' wiring and ECU's aren't interchangeable (specifically after 91 I mean, most of the stuff pre 91 won't accept anything from later models), but most of the sensors are.
I could go on and on, but for the sake of not rambling;
yes, the YJ engine will work, if you bring over the correct parts/only use specific one's and keep most of the TJ stuff, but it won't have the same power if you swap over the cylinder head without shaving to 9.2:1 (but if that's the case, why not go farther and do a stroke job!?!?)
complicated stuff... man...
Correct me if I'm wrong on anything, I tend to be stupid!