spiral array javascript code example

Example: javascript array spiral

function(A){
        let retArray = [];
        let T = 0;
        let B = A.length - 1;
        let L = 0;
        let R = A[0].length - 1;
        let d = 0;
        
        while (T <= B && L <= R) {
            if (d === 0) {
                for (let i = L; i <= R; i++) {
                    retArray.push(A[T][i]);
                }
                T++;
                d = 1;
            } else if (d === 1) {
                for (let i = T; i <= B; i++) {
                    retArray.push(A[i][R]);
                }
                R--;
                d = 2;
            } else if (d === 2) {
                for (let i = R; i >= L; i--) {
                    retArray.push(A[B][i]);
                }
                B--;
                d = 3;
            } else if (d === 3) {
                for (let i = B; i >= T; i--) {
                    retArray.push(A[i][L]);
                }
                L++;
                d = 0;
            }
        }
        
        return retArray;
    }