JSFiddle - React, Tailwind, and code Playground

HTML

<span id="box"></span>

JavaScript

var blocks = [];

function Block(x, y, z) {
    blocks.push(this);
    this.x = x;
    this.y = y;
    this.z = z;
    this.neighbours = function() {
        var n = 0;
        blocks.forEach(function(block) {
            if (block != this && Math.abs(block.x - this.x) <= 1 && Math.abs(block.y - this.y) <= 1 && Math.abs(block.z - this.z) <= 1) {
                n++;
            }
        }, this);
        return n;
    };
}

var b1 = new Block(0, 0, 0);
var b2 = new Block(0, 1, 0);
document.getElementById("box").innerHTML = b1.neighbours();