Instead of naming your variable as name
change it to some other name and you will get a right result.
var myArray = ['Rohan', 'Google', 'Student'];
console.log(myArray.indexOf('Google'));
The reason is there is some global variable called name
whose result is being shown (If you check developer console and search window.name
you can know what that variable is).
var
has global scope in this case pointing to window
object.
And in case of let name
, we should know that let
has local scope within the context so this time name points to your array not global name. Infact ES6 introduced let
, const
to avoid global spacing issue that we were facing with var