Find possible combinations with ALL N elements in an array

Given an array with n elements, I’d like to return possible combinations with all n elements in the array using javascript or any programming language (I’m interested in the algorithm).

Example:

AB -> AB, BA
ABC -> ABC, ACB, BAC, BCA, CBA, CAB
ABCD -> ABCD, ABDC, BACD ... DCBA
ABCDE -> ABCDE ... EDCBA