Very first, the computer has the string as the input.
Case 1: The computer finds the last and first index to be same (or equal) then the if loop here (if (i - j - 1 < new_str.length && new_str[j] != new_str[i - j - 1]) { flag = false; break; }) will not do anything and so the j will get incremented by 1 and this concludes:
Case 2: The computer finds the last and first index are not same or (equal), then this ** ( if (i - j - 1 < new_str.length && new_str[j] != new_str[i - j - 1]) { flag = false; break; })** code will JUST set the flag to false and get terminated as there is "break;". Now, below the code will not execute as it is only if the flag was true. So, the 'i' gets incremented by 1. That concludes:
so in this way the loop just keeps comparing the character Until this condition (j < i - j - 1) becomes false causing flag variable continue becoming true so finally we get to execute the code where flag is true. Now, for example lets say, the string length is 6 and 'i' had been incremented to 8, then this code (new_str += new_str[i - j - 1]) will cause the third element to be added to the new_str then j will increment by 1 so again second element will be added and finally first element will be added to the input string i.e. new_str.