Why javascript indexOf method give me wrong output [duplicate] - javascript

This question already has answers here:
Using the variable "name" doesn't work with a JS object
(4 answers)
When using object destructuring assignment, why is a property "name" cast to string? [duplicate]
(1 answer)
Closed 1 year ago.
If i us "var" to declare array given as below. Why give me 6
var name = ['Rohan', 'Google', 'Student'];
console.log(name.indexOf('Google'));
Otput is 6
If i use "let" to declare array give me right answer why
let name = ['Rohan', 'Google', 'Student'];
console.log(name.indexOf('Google'));
Otput is 1

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

Related

Why can’t name the variable const, but in objects can [duplicate]

This question already has answers here:
Using reserved words as property names, revisited
(4 answers)
Closed 3 years ago.
I wrote.
let const = 10;
and got error.and for objects everything works
let x = {const:10}
What is the difference.
Variables can be used in the same places as you can use the const keyword. If you could use const as a variable name it would be hard to distinguish between the two.
That is not the case for property names; the syntax will always be clear that it refers to a property name.
const is a reserved keyword you cannot use as variable name, so the first one is invalid syntax,
let const = 10
In second example you're using const as key which can be any string
let x = {const:10}
console.log(x)

Creating an array with identifier "name" in Javascript behaves as string, why? [duplicate]

This question already has answers here:
Using the variable "name" doesn't work with a JS object
(4 answers)
Closed 3 years ago.
I was just creating an random array but i fell into an issue when i choose "name" as identifier.
I am writing this code:
var name = ["test", 10, true];
console.log(name);
When i check console then instead of getting an array there it returns a string like "test,10,true"
If i change identifier from "name" to "x" or anything else then it works fine.
Can anyone please let me know what is going on here?
You're assigning to window.name, which calls toString() on whatever you give it. You can't use name as a variable name at global scope.
The global variable name is equivalent to the window.name property, and this is required to be a string. So your assignment is equivalent to
name = ["test", 10, true].toString();
You have to be careful with global variables, to make sure they don't conflict with window properties, since some of these have special behavior.

Why does arrays stored to variable "name" in Chrome convert to strings? [duplicate]

This question already has an answer here:
Why global variable 'name' changes to string? [duplicate]
(1 answer)
Closed 7 years ago.
When I run var name = [1,2,3] in Chrome's console and then access the value of name I get back "1,2,3". Why is this the case?
What you are seeing is a global variable that is part of the window object. This is actually a value the browser uses that reflects the name of the window. (see documentation)
Since window.name is a string getter/setter, your array is being cast to a string. (and the console operates in the "global scope", so var name and window.name are the same value. (if you were nested inside a function, this same behavior would not apply because it wouldn't be the global scope anymore)

Create variable name based on argument sent to function in javascript [duplicate]

This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 7 years ago.
I wanted to create variable name based on value sent to function in javascript.
like following, when i call function variable : variable("zebra"); this should return variable name as zebra1
function create variable(i){
return i+"1";
}
var variable("zebra")="abc";//this line should create variable zebra1 and initialise as abc
Try:
window['zebra'] = 'abc';
The window object holds all the global variables, assuming this is a request for global variables.
To specifically answer your question, you could put return window[i + '1'] = 'abc'; in your variable naming function.
Alternatively, you could create a global (or local) object named variables to hold all your variables:
function whoknows() {
var variables = {};
variables['zebra'] = 'abc';
}
Read more on working with objects at mozilla.org
You can create global variable with
window["zebra"] = "abc";
and use later ether with the same indexer syntax or directly - zebra.

Equivalent of window["myString"] for Local Variable [duplicate]

This question already has answers here:
How can I access local scope dynamically in javascript?
(4 answers)
Closed 8 years ago.
Let say I got a string with the name of the variable I want to create.
If I want to create it with a global scope, I'd use window["myString"] = 100; or global["myString"] = 100; on Nodejs.
Is there a way to do the same but create the variable with local scope? (Ex: Inside a function)
EDIT: Note: The goal is to access the variable with its name directly. Ex: myString
I already know that I could easily create a object that would have the value as an attribute. Ex: obj.myString. But this is not what I'm looking for.
The local scope is not exposed as the global object is.
However...
function someFunction() {
var locals = {};
locals['someLocalVar'] = 'local var';
}

Categories