Junction boxes are the safe way to connect electrical wires. They have clamps that hold the cables and then the wires are connected once the sheathing is stripped from them. The box provides room to make connections as well as providing protection from the connections being hit or pulled.
Home inspectors see this often. Up in an attic a homeowner may have done some work and it can be downright scary. There are no junction boxes or even wire nuts used to twist the ends of the wires together. Plenty of people rely on electrical tape to do the job of wire nuts and junction boxes. This is scary because without solid connections, you can get heat and possibly a fire.
A junction box is secured to the framing or wall and then the cables are secured to the junction box with either cable clamps, or integral clamps built into the box. This will prevent movement of the cable. Electrical tape does not offer this. People usually don’t go in the attic much, but when they do, they can disturb the cables by tripping over them or by moving insulation, etc. Junction boxes can withstand some stress placed on the cables, but electrical tape will typically give and allow the connections to weaken and possibly separate.