Reference Error | javascript - javascript

Can anyone explain how below code works in JavaScript or any other programming language.
What the below referencing is called.
var a=5;
var a=b;
b=10;
console.log(a);
console.log(b);
10 // value of a returned
10 // value of b returned
or
var a=5, b;
var a=b;
b=10;
console.log(a);
console.log(b);
Both returns value as 10. Why is the value of a is assigned to 10 and how?

This will never work.
It will show b is not defined in javascript.

I think you mean this:
var a=5;
var b=a; //changed this from var a=b to var b=a. this is important
b=10;
console.log(a);
console.log(b);
line one declares var a, it equals to five.
line two declares var b, it equals to a, which is five. on ur code u said var a is b (b is never declared)
line three says var b is ten.
then u just logs them.
or if u mean this
var a=5;
var b=a; //changed this from var a=b to var b=a. this is important
a=10;
console.log(a);
console.log(b);
then it's basically the same thing except on line 3, u change value of a to 10. u would think that var b's value will also be 10 but you need to say b=a AGAIN after that

Related

Why is the value of b printed as 23 and not 46?

My code:
var a = 23;
var b = a;
a = 46;
console.log(a);
console.log(b);
Why is the value of b printed as 23 and not as 46?
Output :
a=46, b=23,
In Javascript, Only Objects/Arrays are passed by reference and others are passed by value. As a and b hold integer values they are passed by value.
When var b = a; is executed, b does not "refer" to a. It becomes a number whose value is a's value at this moment.
However, if you use an Object, the attribution will use the reference of a, and not its value:
a = { value: 23 };
b = a;
a.value = 46;
console.log(b);
// console
Object { value: 46 }
Look at this answer. Primitives are passed by values and objects are passed by reference. As a and b are primitives, they are passed by values. And when a is changed that will not be reflected in b.
Because you are giving a value of a, and that is 23. Then you are reassign a to be 46.
in your code first you initialize a with value of 23 and then you assign value of a to b
var a = 23;
var b = a;
a = 46;
console.log(a);
console.log(b);
then you update value of a but not assigning it to b
As in Java-Script Only Objects/Arrays are passed by reference and others are passed by value. As a and b hold integer values they are passed by value.
so updating value of a did not result in change of b value ; if you assign value to b after updating value of a it would result in displaying value of 46 for both a and b
assignment operator means you assign the value of right side to the value of left side so by this statement var b = a; b becomes 23 and once you change a it has no affect on value of b due to lexical scoping of js.

understanding functions and numbers

var a = 3;
var b = 5;
example(a, b);
function example(b, a) {
var result = b - a;
alert(result);
}
My question is looking at this I though the result would be 2 but its negative 2 can someone explain why please?
You've inverted the arguments in your function definition.
While you are calling (a,b), you are receiving (b,a). This means that you are passing:
example(3,5)
and receiving:
(b=3, a=5)
You then return:
(b-a) or (3-5)
which is -2.
The actual names of the parameters in the function don't matter. You're getting confused because wherever you found the example- they cleverly reversed the order of b and a. However the names in the parameter are just used in the function scope and don't affect variables of the same name outside of it. For example:
var a = 3;
var b = 5;
example(a, b);
function example(bacon, eggs) {
var result = bacon - eggs;
alert(result);
}
Would also return -2 because the first parameter we pass through to example is a (3) and the second is b (5) and 3-5 = -2. Doesn't matter what the name of the parameters in example actually are named- its important to keep this in mind.
There is no problem with your code. The problem is with your lecture of the code. The result is in fact, -2. Debug your code in Chrome Debugger or similar
var a = 3; // a equals 3
var b = 5; // b equals 5
example(a, b); // Replacing variables this is the same as example(3,5)
// Changing variables names so you don't get mixed up
function example(c, d) {
// Since you called example(3,5) then c = 3 and d = 5
var result = c - d; // This results in -2
alert(result);
}
Don't be confused with the variable names because in js, it works by the order of your function argument not by variable name of functions. To get what you want as output i.e 2, try like this.
example(b = 5, a = 3);
function example(b, a) {
var result = b - a;
alert(result);
}
If you've any confusion go to http://www.pythontutor.com/javascript.html and see what is happening under the hood.

Why is the output 2 (and not undefined)?

I am learning JavaScript. I wrote a simple code snippet:
var a=5;
var a=7;
console.log(a);
This outputs 7 and understandably so. However:
var b=2;
var b; //!=undefined?
console.log(b);
outputs 2. I was expecting undefined since variables are initialized to undefined on initialization. What am I missing?
Thanks!
Because of hoisting. Your code will be interpreted by browser like:
var b;
var b;
b = 2;
console.log(b);
For more information: https://www.w3schools.com/js/js_hoisting.asp
var initializations are hoisted, and duplicate initializations are ignored. Variable assignments are not hoisted. So, to the interpreter, your first code looks more like:
var a;
a = 5;
a = 7;
console.log(a);
and your second code looks like:
var b;
b = 2;
Just a plain "var b" is effectively ignored if b has already been declared as a var earlier.

array re/assignment reference changes

In javascript arrays are objects therefore passed by reference. So
var a = ["a"];
var b = a;
console.log(b);
a[0] = "wtv";
console.log(b);
will change b value.
what I don't understand is why
var a = ["a"];
var b = a;
console.log(b);
a = ["wtv"];
console.log(b);
is not changing b value ? what is the reasoning behind this ?
Because the value in memory a points to is changed by the assignment a = ["wtv"];.
while in the first example you change a part/property of a, while the object in memory a points to stays the same.
An image to explain it:
It's because you're b is just copying a reference to a.
So they have copies of the same reference, but they each have their own copy of that reference.
var a = ["a"];
// b now holds a copy of the reference from a
var b = a;
// when you change a, b is unaffected since it has an independent reference
// a now points to a new location in memory
// a has a new reference, whereas b still has the reference from before
a = ["wtv"];
However, since the two variable do have the same reference, even though they are copies, you can change data within the object or array itself and have it affect both variables.
Take this for example:
// a points to a location in memory
var a = [];
// we give a some value
a["foo"] = 'bar';
// b now has a *copy* of a's location in memory
var b = a;
// since b shares the same location in memory as a, it has the same foo value
console.log(b.foo); // => bar
// we update a's foo value
a["foo"] = 'baz';
// since b still shares the same location in memory as a,
// it's pointing to the same foo from a, therefore it's also affected
console.log(b.foo); // => baz
#Hidde has a great image that helps visualize what's going on behind the scenes with where the memory is pointing.
With a = ["wtv"]; you assign brand new array to variable a. It has nothing to do with it's former reference.
An array in JavaScript is also an object and objects are always passed/assigned by reference. Thus both variables have a reference to the same object, thus making a change in one will reflect a change in another as both are pointing to the same value.
Whereas in the later case, you are assigning a new value to the var a and this will be stored at different memory location and not on the one on which b is stored, it is similar like doing
var a = 5;
var b = a;
a = a - 1;
alert(b); // alerts 5
alert(a); // alerts 4

Javascript multiple assignment statement

I saw that line in some code
window.a = window.b = a;
How does it work?
Does the following always return true?
window.a == a
it means
window.b=a;
window.a=a;
OR You can say.
window.b=a;
window.a=window.b;
two assignments in a single statment
and
And one more thing
window.a==a right?
yes this is Right. It will return true
This assignment runs from right to left, so at first 'window.b' will be assigned with 'a' value then 'window.a' will be assigned with 'windows.b' value.
You can break this multiple assignment like this and get the same results:
window.b=a;
window.a=a;
You should be also aware of something like scoping.
If you run this code in global scope, eg simple script like this:
<script>
var a = 10;
window.a = window.b = a;
</script>
window.a==a is true, because 'a' and 'window.a' are the same variables. 'a' is really a property of 'window' object. All global variables are properties of 'window' object. Knowing that you can write you code like this, and this code will be corresponnding:
<script>
var a = 10;
a = b = a;
</script>
But if you put this code in a function, it runs in function scope, eg:
<script>
function ex() {
var a = 10; // this is local variable
window.a = window.b = a; // this time window.a != a
}
</script>
Same as:
window.b = a;
window.a = a;
And no, window.a and a is not always equal. Typically it is only equal on the global scope in a web browser JavaScript interpreter.
The a and b properties on the window are being assigned to the value of a. Yes, if this code is executed in the global scope, a and window.a are the same.
var a = "foo";
//window.a and a are the same variable
window.a = "bar";
a; //bar
function f(){
var a = "notfoo";
//window.a is a different variable from a, although they may take the same value
window.a = "baz";
a; //notfoo
}
It's the same like:
window.b=a;
window.a= window.b;
window.a == a will be true in this case, after the statements above. There are some cases that it will be false, for example: when a is a global variable.
And one more thing: please find more informative title for your question next time.
Actually, window.a==a can be false if a has the value Number.NaN. That's because Number.NaN is not equal to any value, including itself.

Categories