Strange behaviour in browser: var name converts everything to string [duplicate] - javascript

This question already has answers here:
What is the global variable called 'name' in javascript? [duplicate]
(2 answers)
Using the variable "name" doesn't work with a JS object
(4 answers)
What is the difference between "let" and "var"?
(39 answers)
Closed 5 years ago.
since today I believe in ghosts.
Can someone explain the following JS behaviour with Chrome (Version 59.0.3071.115) or Firefox (54.0 (64-bit)) ?
Why?
var name = 10;
console.log(typeof name); // string
console.log(name); // "10"
Same with let works:
let name = 10;
console.log(typeof name); // number
console.log(name); // 10
What I believe is, that window.name is used and it transforms any input to string. But then, why is var name not a local variable and hides the global variable?
Thank you.

Related

Caseinsensitive variable in javascript [duplicate]

This question already has answers here:
Access JavaScript property case-insensitively?
(19 answers)
"Variable" variables in JavaScript
(9 answers)
Closed 11 months ago.
I am trying to fetch value using below statement
var variable_2 = context.getVariable(variablefetch);
My requirement is to fetch this variable_1 irrespective of case.
For example if value of this variablefetch could be stored as
variablefetcH = typea
VAriablefetch = typea
VARIABLEFETCH = typea ....etc
So irrecpective of what i pass (in above case variablefetcH,VAriablefetch,VARIABLEFETCH) it shoule be able to fetch value tyea

output of the javascript snippet? javascript variable declarations [duplicate]

This question already has answers here:
How JS hoisting works within functions?
(4 answers)
Why a variable defined global is undefined? [duplicate]
(3 answers)
Closed last year.
My first guess is that the following code has the output of 10 and 100. However, after running the code I get undefined and 100 and I don't understand why the function doesn't see the first declaration of x..
var x = 10;
function f() {
console.log(x);
var x = 100;
console.log(x);
}
f();

javascript confusing variable syntax [duplicate]

This question already has answers here:
Javascript - Getting and setting properties on primitives implicitly creates object wrappers
(3 answers)
How is almost everything in Javascript an object?
(6 answers)
adding properties to primitive data types other than Array
(2 answers)
Closed 1 year ago.
I am new to JS, and JS syntax is definitely confusing to me as I'm used to other languages instead.
const x = 5;
x.yy = 3;
console.log(x.yy); // undefined
How is x.yy = 3; a valid syntax?
and why is x.yy undefined when x.yy =3; took in place?

Get variable by variable [duplicate]

This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 1 year ago.
let varone = "vartwo";
let vartwo = "answer";
And I would like to get "answer". But I cannot do the following:
console.log(vartwo);
How can I get "answer", but not by including "vartwo" in my code?
I think one way to resolve it, is by using javascript objects, as shown:
obj={
varone:"vartwo",
vartwo:"answer"
};
console.log(obj[varone]) //outputs "answer"

How insert variable in JS [duplicate]

This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 9 years ago.
I need help, how to insert variable n as XX. Variable n is number, for example 3 and I need to get this number to document.form1.any3.value; is it possible? Thanks for advice
function pokus(n){
var any1 = document.form1.anyXX.value;
}
Use square bracket notation:
function pokus(n){
var any1 = document.form1['any'+n].value;
}

Categories

Resources