isBalanced function for checking if string with brackets is balanced ([{}]) I wrote simple function for checking if string is balanced (with brackets ([{}])). It takes a string argument and need to return boolean value (true if string is balanced and false if not).



const isBalanced = (string) =&gt; {
  const arr = string.split('');
  let open = [];
  
  const openBrackets = {
    '(': true,
    '[': true,
    '{': true,
  };
  
  const closedBrackets = {
    ')': '(',
    ']': '[',
    '}': '{',
  };
  
  for (let i = 0, length = arr.length; i &lt; length; i++) {
    if (openBrackets[arr[i]]) {
      open.push(arr[i]);
    } else if (closedBrackets[arr[i]] &amp;&amp; open.pop() !== closedBrackets[arr[i]]) {
      return false;
    }
  }
  
  return !open.length;
}

console.log(isBalanced('') === true);
console.log(isBalanced('()') === true);
console.log(isBalanced(')()') === false);
console.log(isBalanced(')(') === false);
console.log(isBalanced('()(()()()())') === true);
console.log(isBalanced('[](){}') === true);
console.log(isBalanced('[({})]') === true);
console.log(isBalanced('[(]{)}') === false);
console.log(isBalanced('const sth = () =&gt; { animal.sleep();') === false);
console.log(isBalanced('const sth = { bark: animal() }') === true);




Do you think that i could improve it somehow? Can you see any mistakes?
openBrackets can be a Set

openBrackets is only used to test if a character is an opening bracket. So, it can be declared like this:

const openBrackets = new Set(['(', '[', '{']);


and the test will become: 

if (openBrackets.has(arr[i])) { ... }


Use for...of to iterate

The for ... of syntax avoids having to work with indices, as in this case you don't need the i variable at all (except to dereference the correct character from arr). Furthermore, you can iterate the string directly, without having to .split('') it first:

for (let c of string) {
  if (openBrackets.has(c)) {
    open.push(c);
  } else if (closedBrackets[c] &amp;&amp; open.pop() !== closedBrackets[c]) {
    return false;
  }
}


Rename closedBrackets

Reading the name of this variable I would expect some sort of collection of brackets, not a mapping/correspondence between closing and opening brackets. Therefore, I would give it a different name. Something like bracketCorrespondence might work, although there might be some better names for it out there.

So, here's a version of the code with these suggestions applied:



const isBalanced = (string) =&gt; {
  let open = [];
  
  const openBrackets = new Set(['(', '[', '{']);

  const bracketCorrespondence = {
    ')': '(',
    ']': '[',
    '}': '{',
  };
  
  for (let c of string) {
    if (openBrackets.has(c)) {
      open.push(c);
    } else if (bracketCorrespondence[c] &amp;&amp; open.pop() !== bracketCorrespondence[c]) {
      return false;
    }
  }
  
  return !open.length;
}

console.log(isBalanced('') === true);
console.log(isBalanced('()') === true);
console.log(isBalanced(')()') === false);
console.log(isBalanced(')(') === false);
console.log(isBalanced('()(()()()())') === true);
console.log(isBalanced('[](){}') === true);
console.log(isBalanced('[({})]') === true);
console.log(isBalanced('[(]{)}') === false);
console.log(isBalanced('const sth = () =&gt; { animal.sleep();') === false);
console.log(isBalanced('const sth = { bark: animal() }') === true);