How does "a^=b; b^=a; a^=b;" swap the values of the variables [duplicate] - javascript

This question already has answers here:
Swapping two variable value without using third variable
(31 answers)
How does XOR variable swapping work?
(11 answers)
Closed 10 months ago.
I found out a neat trick to swap the values of two variables without having to create an auxiliar variable. I'll use javascript to illustrate it, although I'm guessing it's language agnostic.
let a = 1;
let b = 2;
a^=b; b^=a; a^=b;
console.log(a); // Prints 2
console.log(b); // Prints 1
Can someone explain how does the swap happen? What does ^= do?

Related

We cannot use 1.toString(), but we can use `let a = 1; a.toString()`; Why? [duplicate]

This question already has answers here:
Why can't I access a property of an integer with a single dot?
(5 answers)
Calling member function of number literal
(3 answers)
Closed 2 years ago.
Is this means assignment in js of primitive makes primitive become object automatically?
let a = 1 just be transferred to let a = new Number(1), we know Number is a function, and it's prototype has toString, this makes sense? Is it right?
Finally, we know the primitive of js is stored in stack memory, but if we can only get object by assignment, so is it means only pointer exists in stack? I am confused. Thanks for your answers if you can help me.
You can not call Number.prototype methods directly from a number in digits form, e.g. 1 2 3 4 5 6 7 8 9, you have to wrap them in parentheses.
// This doesn't work
console.log(1.toString());
// This works
console.log((1).toString());

empty an array passed to a function [duplicate]

This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
Is JavaScript a pass-by-reference or pass-by-value language?
(33 answers)
Closed 3 years ago.
I am trying to empty an array passed to a function within javascript:
var a = [1,2,3];
function emptyArrayNo(ar) {
// the following does not empty the global array
ar = [];
}
function emptyArrayYes(ar) {
// any of the following 3 methods empties the global array
ar.length = 0;
//ar.splice(0, ar.length)
//while(ar.length > 0) {ar.pop();}
}
emptyArrayNo(a);
console.log(a); // returns [1,2,3]
emptyArrayYes(a);
console.log(a); // returns []
All the three methods within emptyArrayYes() seem to work, which I think is due to the reference to the array passed not being modified within the function. However when we set the array to [] within the function, I believe the reference changes therefore causing issues i.e. not being seen at the global scope. Can someone please clarify what is going on here? Thanks.

Why do this returns a non expected result? [duplicate]

This question already has answers here:
Why is [1,2] + [3,4] = "1,23,4" in JavaScript?
(14 answers)
Closed 4 years ago.
So I'm learning about the reduce method and I write this code...
function getSum(x,y){
return x+y
}
var arraySum = function(array) {
return array.reduce(getSum)
};
arraySum([1,[2,3],[[4]],5])
But it actually returns a string of the elements all-together...
I'm actually trying to sum them all... I expected the result to be 15 instead of "12,345"
What's happening? what am I doing wrong?
Your problem is you're adding actual arrays together, not the content within the arrays. Add a counter, and an incrementational variable like i inside the variable arraySum, and call the position of i, incrementing it every time, it should fix your problem.

Javascript variable value change [duplicate]

This question already has answers here:
Is JavaScript a pass-by-reference or pass-by-value language?
(33 answers)
Closed 6 years ago.
can someone explain to me why the variable old is still assigned to 1 instead of 8? I thought the variable old will change to 8 because I have assigned a new value to array [0]. I thought the equal sign holds the transitivity property.
array= [1,2,3,4,5];
var old=array[0];
array[0]=8;
console.log(old);//1
Thanks for your help
var old=array[0];
Here you're assigning the value of array[0] to the variable old
This doesn't change array[0]. This basically means that this is passed by value and not by reference.

Why can't I do 1.toString() but I can do var a = 1; a.toString() [duplicate]

This question already has answers here:
Usage of toString in JavaScript [duplicate]
(3 answers)
Closed 6 years ago.
When I do
1.toSting()
I get an error, but
// javascript
var a = 1;
// or c#
int a = 1
a.toString()
works. Why is it that when a number gets assigned to a variable, it get some special functions?
The . is interpreted as you want a decimal/floating-point literal, not invoking a member.
You can do this in JavaScript
// Option 1
(1).toString();
// Option 2
1.0.toString();
// Option 3
1..toString();
In C#, it appears your only option is (1).ToString(), but the lexer might be smart enough not to need them.

Categories