This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
Closed 4 years ago.
My Code (Javascript):
var num = 0;
function change(){
var num = 10;
}
change();
document.write(num);
The result should be 10 but it is showing 0. Why?
If the code is wrong, what is the correct way to do this?
You are setting another local variable called num.
Just change the inner var num to num.
Related
This question already has answers here:
Redeclaring a javascript variable
(8 answers)
Best place to declare variables in a function [duplicate]
(3 answers)
Closed 4 months ago.
If I am going to use the below example, should I declare a variable (initialize it with zero value in JS) or just use it normally later?
// Currency Converter
var currencyOne = 100; // $
var currencyTwo = 0; // Shall we do this to use as below for printing the result out or no need in JS to declare it with zero value?
var exchangeRate = 19.66; // EGP for Example
function CurrencyConverter(amount, rate) {
return amount * rate;
}
var currencyTwo = CurrencyConverter(currencyOne, exchangeRate); //I mean to use it here without any declaration first
console.log(currencyTwo);
First, you should use let and const, never var.
As to where to declare variables, there is absolutely no point declaring variables earlier than needed, it just makes your code more complicated to understand.
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();
This question already has answers here:
Redeclaring a javascript variable
(8 answers)
Closed 5 years ago.
Why is the global variable some_var changed inside the if block in this example?
<script>
var some_var = 0;
var i = 5;
if (i>2)
{
var some_var = 2;
}
else
{}
console.log(some_var);
</script>
I'm guessing that if you remove the 'var' inside the if block it works properly. Must be the environment checking for initializations before running the code.
This question already has answers here:
What is a 'Closure'?
(22 answers)
How do JavaScript closures work?
(86 answers)
Closed 5 years ago.
function wrapValue(n) {
var localVariable = n;
return function() { return localVariable; };
}
var wrap1 = wrapValue(1);
var wrap2 = wrapValue(2);
console.log(wrap1());
// → 1
console.log(wrap2());
// → 2
In this example how is it allowed to access localVariable, which is local to wrapValue() function, from the global environment?
The book, included this example, stated the reason as follows:
"multiple instances of the variable can be alive at the same time".
But I didn't understand.
Your function sets a private value (localVariable) when called and returns it immediately so you can assign it to a another variable without change the private var inside function.
This question already has answers here:
How to observe value changes in JS variables
(3 answers)
Closed 5 years ago.
function getPercCalculated(x){
return (x*9.3)/100;
}
var x = 10;
var perx = getPercCalculated(x);
Instead of this I would like to call the getPercCalculated using dot operator like
var perx = x.getPercCalculated()
Can someone help me..!!
Number.prototype.getPercCalculated= function(){
return (this*9.3)/100;
};
This will attach the getPercCalculated to every number in your code tough