Infinite loop in browser's console [duplicate] - javascript

This question already has answers here:
Using the variable "name" doesn't work with a JS object
(4 answers)
Closed last year.
Can someone explain why result of this is infinite loop??
var name = true;
var soloLoop = function () {
while (name) {
console.log(name);
name = false;
}
};
soloLoop();
Note 1: Can be reproduced only in browser's console.
Note 2: Is reproducible only with variable "name".

When you declare variables in the global scope, as you're doing here, they're actually contained as properties on the global object, in this case window. window.name is something that already exists, and can only be set to a string.
When you do:
var name = true;
It's actually setting the window.name to "true". Same for name = false - it sets it to "false". Since "false" is "truthy", the while loop will never exit.

Related

In Javascript, how does the window object see property values before the property is assigned a value? [duplicate]

This question already has answers here:
Is Chrome’s JavaScript console lazy about evaluating objects?
(7 answers)
Weird behavior with objects & console.log [duplicate]
(2 answers)
Closed 1 year ago.
From my understanding, all properties created are given the value of undefined after the execution phase of the Execution Context - then, during the creation phase, the JS engine goes through the script, line by line, and assigns a value if it finds an = operator. With this in mind:
console.log(window);
var myVar = 1;
From the above snippet, why does the myVar property, within the global object, show a value of 1? I would have thought it would show a value of undefined, as this is what it was set to during the execution phase? If I try to access the property directly, like:
console.log(window.myVar);
var myVar = 1;
I DO see the value as undefined... it's only when I log the entire global object I'm seeing the value as 1. Am I missing something here?
Note - this is simply for learning purposes.
this has nothing to do with the window object, this is called hoisting
Hoisting refers to the process whereby the interpreter appears to move the declaration of functions, variables or classes to the top of their scope, prior to execution of the code.
another example would be
function test(){
console.log(myVar);
var myVar = 1;
}
test()
this would still log myVar as undefined. while omitting the myVar would throw an Exception..
Edit:
this only happens in the chrome console, it would be undefined otherwise.

JavaScript function not working when passing variable instead of hard coded value [duplicate]

This question already has answers here:
Using a string to access a variable
(3 answers)
Closed 3 years ago.
I have a JS function as below
// A simple array where we keep track of things that are filed.
filed = [];
function fileIt(thing) {
// Dynamically call the file method of whatever 'thing' was passed in.
thing.file();
// Mark as filed
filed.push(thing);
}
Now, function fileIt(thing) is working well when called as below
fileIt(AuditForm);
Whereas, its giving error at line thing.file(); when i am trying to pass a variable like below
var formID = obj.id;
fileIt(formID);
Variable formID has same value and i.e. "AuditForm" what's wrong here. Kindly suggest.
If obj.id is the string AuditForm, then you have no choice but to use dynamic property notation on the global window object, or use eval if you didn't declare AuditForm with var on the global scope:
If you declare AuditForm with var on the global scope:
fileIt(window[formID]);
If you don't:
fileIt(eval(formID));
Do note that eval is a very poor option, as if obj.id can be interpreted as other code, e.g. another eval call which will be evaluated, then malicious operations can be performed. Example:
const obj = {
id: "eval('alert(\"Inside an eval script!\")')"
};
eval(obj.id);

Javascript Closure -Local variable nested function [duplicate]

This question already has answers here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
Closed 6 years ago.
I am trying to use a variable x defined inside a function P whose value I am trying to set in another function. It always comes undefined.
I tried applying my mind to use closure but it's just going off my head. It does not gives me a string rather a object.
The logic is as follows.
function P(i){
var x;
if(i!=null){
//this pulls the data correctly and i could see it in network tab response.
var dataFromQuery=widgets.DATA.create({
url:"abc/cde",
queryTemplate :"/query"+i+ "?"
});
//we query the data and set the value as per the logic.
dataFromQuery.query(function(data){
if(data[0].name){
x=data[0].name; // this stays undefined , and i understand this is a new local variable x.Also the value is here and comes fine here so no issues with the data but i want to set it to the variable defined outside as x.
}
})
}
else{
x="somehardcode";
}
};
I have tried storing the result dataFromQuery.query(function(data){ to a var and then setting it to x but it again comes as a object, which i need as a string.
Thanks
I think you're looking for something like this:
var P = (function() {
var x;
function _P(i) {
//your internal logic here
}
return _P;
})();
Both xand _P are wrapped in the enclosure and scoped only to the auto-executed anonymous function. _P is returned and available as var P in the outer scope, but x will remain hidden.

Getting the value of variable named from a second variable in JavaScript [duplicate]

This question already has answers here:
How to find JavaScript variable by its name
(7 answers)
Closed 8 years ago.
I need to assign a variable name from a second variable, then alert the value of the first variable in JavaScript.
Below is an example of what I am trying to achieve.
window.foo=0;
window.bar="window.foo";
//want to set an alert for window.bar in a way that returns 0
alert(window.bar); //returns window.foo
alert(Number(window.bar)); //returns window.NaN
In the example above, I am looking to alert the value 0. How would that be accomplished? Thank you.
If they're global variables (as those are), you can simply use "foo" rather than "window.foo" when looking it up on window:
var name = "foo";
window.foo = 0;
alert(Number(window[name])); // 0;
But global variables are a Bad Thing(tm).
To do this without globals, use your own object:
var name = "foo";
var obj = {};
obj.foo = 0;
alert(Number(obj[name])); // 0;
Both of the above work because in JavaScript, you can refer to an object property either with dot notation and a literal (obj.foo), or with bracketed notation and a string (obj["foo"]), and in the latter case, the string can be the result of any expression, including a variable lookup.

Do Javascript variable assignments work by reference? [duplicate]

This question already has answers here:
JavaScript by reference vs. by value [duplicate]
(4 answers)
Closed 8 years ago.
I have a question about referencing objects in javascript.
Say I have a variable that is some object (lets say json) and it is called objOne - (var objOne = someJSONObject;).
If I go ahead and declare
var objTwo = objOne;
will I have two references to the same Object? Kind of like a c pointer?
To sum it up :
assignements are done by value
you never manipulate objects, only object references
This means that
you'll have two references to the same object (you can check that by changing a property of the object)
when you're passed in a variable the value of a primitive, changing your variable doesn't change other variables
EDIT : as it's a duplicate I'll delete this answer in a minute to allow a proper closing if there's no other answer. Please vote to close.
Yes, objects are passed by reference:
function changeVal(obj){
obj.value = "bar"
}
(function checkRefs(){
var myObject = {
value: "foo"
};
alert(myObject.value);
changeVal(myObject);
alert(myObject.value);
})();

Categories

Resources