Javascript - get value of variable without declaring a second one - javascript

var i = 5;
test();
function test() {
let i = 3;
alert(i);
}
The above example alerts the value of the second variable that is 3. How can I code it to alert the value of the first while keeping the two with the same name? Thanks

Use outside scope
var i = 5;
test();
function test() {
// let i = 3;
alert(i);
}
or make it a function parameter
// var i = 5;
test(5);
function test(i) {
// let i = 3;
alert(i);
}

Use it's global namespace, i.e. the global window variable.
var i = 5;
test();
function test() {
let i = 3;
alert(window.i);
}

What you probably want is this:
var i = 5;
test(i);
function test(x) {
alert(x);
}

Related

How call function using variable in js?

for example, I have code like this:
var test = function() {
return Math.random();
}
var randomNumber = test();
// I want to call test() via the randomNumber variable,
// use this variable on another function or etc...
// for example here
for (var i = 0; i < 5; i++) {
console.log(randomNumber); // and it should be show 4 random numbers
}
Is this possible?
Thanks to advance.
I believe you want to assign the function reference to randomNumber instead of calling the function and assigning its return value.
var test = function() {
return Math.random();
}
// Assign test to randomNumber, but don't call it here
var randomNumber = test;
for (var i = 0; i < 5; i++) {
console.log(randomNumber()); // call it here
}
You could assign the value returned by the function to the variable inside the loop and then display it.
Kinda like the code below
// define function
var test = function() {
return Math.random();
}
// define variable
var randomNumber;
// loop
for (var i = 0; i < 5; i++) {
// invoke function, assign value to variable
randomNumber = test();
// output it to console.
console.log(randomNumber);
}
UPDATE
If you do not want to write code to 'loop' thru your array, you can use a fake array and map the same function to it.
You are not looping thru it yourself but looping thru the array is being performed nonetheless.
// array of fake values
var iterations = [0, 1, 2, 3, 4];
// returns an array of random numbers
console.log(iterations.map(function(elem) {
return Math.random();
}));

Javascript Function not executing/returning value

I have a function and I want to see what the value of index is. It gives me 0 however. I thought that was wierd so I put a console.log in function() to see if it was executing or not and I didn't recieve an output which tells me that function() isn't getting called. Not sure what I'm doing wrong.
function jsTest() {
var index = 0;
var counter = 0;
var obj = {};
obj.index = index; //obj.index = 0 at this point
var func = function () {
for (index = 0; index < 10; index++) {
counter += 2;
console.log(counter); //Doesn't execute for some reason
}
obj.index++;
};
obj.func = func; //executes function()
this.index++;
return index;
}
var x = jsTest();
console.log(x);
obj.func = func;
doesn't actually call func, it assigns the property func for obj to be the function func. If you want to call func, you should add parentheses afterwards, like
obj.func = func();

Passing global javascript array to another function

I have a small javascript which has a globally declared array. The values for that array are filled inside the function foo() as given below:
<html>
<head></head>
<body>
<script>
var myArray = [];
function foo() {
var j = 5;
for (var i = 0; i < j; i++) {
myArray.push(i+1);
}
}
function bar() {
alert(myArray);
}
</script>
</body>
</html>
When I trying to access that array in another javascript function bar(), the values of array are null. How can I fix this?
you have defined the function but never called it.
Try calling foo() and bar() like this
var myArray = [];
function foo() {
var j = 5;
for (var i = 0; i < j; i++) {
myArray.push(i+1);
}
}
function bar() {
alert(myArray);
}
foo();
bar();
JSFiddle
If you are going to call bar() directly then.
<script>
function foo(myArray) {
var j = 5;
for (var i = 0; i < j; i++) {
myArray.push(i+1);
}
return myArray;
}
function bar()
{
alert(foo([]));
}
bar();
// or
alert(foo([]));
</script>
Try to avoid as many global variables as you can,

How can I prevent this infinite loop, if I don’t know content of function

How can I isolate my variable from variable in this function, if his creator forgot for var keyword?
for (var i = 0; i < 4; i++)
{
test();
}
function test()
{
i = 0;
}
Same idea than previous answer using scoping but a better way would be to use IIFE:
(function () {
for (var i = 0; i < 4; i++) {
test();
}
})();
http://jsfiddle.net/8vBc5/
put your for loop in a separated scope:
in a function.
function test(){
i = 0;
}
function trial(){
for (var i = 0; i < 4; i++){
test();
}
}
trial();
That way only the code and functions inside the trial function can access variables declared at that level.

Closures in Javascript: assigning local variable doesn't work

Just when I thought I understood closures...
The following code snippet:
function f() {
var a = [];
var i;
for (i = 0; i < 3; i++) {
a[i] = function () {
var x = i;
return x;
}
}
return a;
}
var a = f();
console.log(a[0]());
console.log(a[1]());
console.log(a[2]());
prints out 3, 3, 3. I don't understand why. I'm copying the value of 'i' to the local variable x, so there should be three x's: x0=0, x1=1. x2=2. How are all of them reading the final value of i?
Your problem is caused by each a[i] being, in fact, a closure. They all share the same i, which is evaluated when each a[i] is called, not when the loop executes. You need to create each closure with a separate context. For instance:
function f() {
var a = [];
var i;
for (i = 0; i < 3; i++) {
a[i] = makeClosure(i);
}
return a;
}
function makeClosure(i) {
return function () {
var x = i;
return x;
}
}
Even though the value of i changes in your for loop, it's still the same i variable. You need to shadow i in that scope and effectively pass it by value:
for (var i = 0; i < 3; i++) {
(function(x) {
a[x] = function() {
return x;
}
})(i);
}

Categories

Resources