problem with data structures and algo in js (beginer level)

I am currently learning data structures and algo fro colt stelle’s course on udemy, and there was a function that he constructed. I cant understant how that line of code works.

   function same(arr1, arr2) {
        if (arr1.length !== arr2.length) {
            return false;
        let frequencyCounterl { };
        let frequencyCounter2 { };
        for (let val of arr1) {
            frequencyCounterl[val] = (frequencyCounter1[val] || 0) + 1
        for (let val of arr2) {
            frequencyCounter2[val] = (frequencyCounter2[val] || 0) + 1
        for (let key in frequencyCounterl) {
            if (!(key ** 2 in frequencyCounter2)) {
                return false
            if (frequencyCounter2[key ** 2] !== frequencyCounter1[key]) {
                return false;
        return true;

So what this algo does, it tries to compare 2 arrays. If array b is equal to the square of each number in array a, then it should return true, else it will return false.

If i do [1,2,3,4,5] [1,4,9,16,25], it will return true

whats bugging me is this line:

 frequencyCounterl[val] = (frequencyCounter1[val] || 0) + 1

I know that this line is making a key value pair, but i want to know how this syntax works so that i can wrap my head around it.


Lets decompose frequencyCounterl[val] = (frequencyCounter1[val] || 0) + 1. What we are doing is

const a = frequencyCounter1[val]
const b = a || 0
const c = b + 1
frequencyCounterl[val] = c

Here a is assigned to the value of frequencyCounter1 found at val. If there is no value at val then a is going to be undefined.

Lets move to b. b could be written:

let b
if (a)
   b = a
   b = 0

the || is a shortcut for that. If a is good use it, or, in any other case (like if a would be undefined) we will set b to 0. || is the or boolean operator.

Alright, last move. We now assign to c the current value of b plus 1.