I have a array and i want to toggle its value on 0 index on button click between 'x' and 'a' but this is not working.I can not understand what i am doing wrong i am new in JavaScript.
function toggle() {
let array = ['x', 'r', 'r'];
let i = 0;
if (array[i] == 'a') {
array[i] = 'x';
}
else {
array[i] = 'a';
}
console.log(array)
}
<button onclick='toggle()'>Toggle</button>
expected output
["a", "r", "a"]
["x", "r", "a"]
["a", "r", "a"]
["x", "r", "a"]
but got
["a", "r", "a"]
["a", "r", "a"]
["a", "r", "a"]
["a", "r", "a"]