Define Variables like a, b, c [duplicate] - javascript

This question already has answers here:
What does the comma operator do in JavaScript?
(5 answers)
Closed 3 years ago.
When I Define variables in JS like this:
var a, b, c = 10;
I found out when call a,b,c this phrase (Or any other term that can be used) gives 10 value.
Now, i'm wondered and my question is what is that? and what type of this phrase? (Returned Number in console)
What is its use?
var a, b, c = 10;
a,b,c; //10
a, b,c; //10

The comma has 2 different means depending on context.
You use it in both these ways in your code sample - this is probably causing some confusion.
1) Multiple variable assignment using var
var a,b,c = 10;
Here the var keyword is used to assign multiple variables on a single line, in your case you only assign the final variable c, and so a and b are undefined.
Here is an example which makes its usage clearer:
var a = 1, b = 2, c = 3;
a; // 1
b; // 2
c; // 3
2) JavaScript Comma Operator
a,b,c; // 10
The JavaScript comma operator evaluates a comma separated list of expressions and returns the result of the last one.

Related

if a=2 and b=3 then how a && b = 3 rather than true? [duplicate]

This question already has answers here:
Why don't logical operators (&& and ||) always return a boolean result?
(9 answers)
Closed 5 years ago.
There is one interview question below.
The logical AND of two truths should be true. But the output is 3. Why?
var a = 2;
var b = 3;
var c = a && b; // value of c = 3
console.log(c);
Check this out.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators
If you use && with non-boolean values, it returns the first element if it can be converted to false. If it cannot converted to false, it returns second element
Returns a if it can be converted to false; otherwise, returns b.

Why don't parameters retain their value outside of a function? [duplicate]

This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
Closed 6 years ago.
The following JS code behaves oddly:
var c = 0;
var a = 0;
myTest(8, 5);
function myTest(a,b) {
console.log(a);
a++;
c++;
console.log(a);
return a;
}
console.log(a);
console.log(c);
https://jsfiddle.net/hwns1v4L/
If I take the "a" parameter out of the function, "a" increments by 1 and the third console log returns "a" as 1. Totally normal. But if I keep the "a" as a parameter in myTest function (as in the code above), it gets a value of 8, increments by 1 to 9, and the third console log returns a zero.
What is the explanation for this weird behavior? I am sorry if this is explained in another thread; I am too new for JS to produce really good google queries or understand advanced answers.
What is the explanation for this weird behavior?
Because in Javascript variables are function scoped.
You never passed a and b to myTest method. You passed 8 and 5, so a and b which were part of myTest signature got new scope. a became 8 and b became 5 inside myTest.
Values of a and b inside myTest will not be used outside since their scope is limited to myTest.
Inside your function, you have a local a parameter. So any changes you make to that value, they will not reflect your globally defined a. Since you did not create a c variable or parameter inside the function, you will be changing the global c value.
var c = 0; // Global c
var a = 0; // Global a
var b = myTest(8, 5); // Store the value of the local a from the function return.
function myTest(a,b) {
console.log(a); // This a is a local reference (8)
a++; // Increment local a
c++; // Increment global c
console.log(a); // Print local a (9)
return a; // Return local a
}
console.log(a); // Print global a (0)
console.log(c); // Print global c (1)
console.log(b); // Print returned value (9)

Storing value in two variables and changing one affects both variables in Javascript [duplicate]

This question already has answers here:
How does variable assignment work in JavaScript?
(7 answers)
Closed 7 years ago.
I have an object "a" and would like to duplicate it to "b"
When I delete an element in "a" why is "b" also being affected?
var a = {'apple':1,'orange':2,'grapes':3}
var b = a
console.log(a,b)
delete b.apple
console.log(a,b)
Now a and b are the same. I only want the element in b to be deleted.
How can I do this
Objects and arrays in javascript are references. So when you do:
var b = a;
You're making another pointer to object a. You're not copying a.
If you want to make a copy of an object you can use Object.create:
var b = Object.create(a);

Array behavior confusion [duplicate]

This question already has answers here:
Why does [5,6,8,7][1,2] = 8 in JavaScript?
(3 answers)
Closed 7 years ago.
In a coding test I have recently encountered a question that asks me to find out what will be printed in console. The question is below. I did not understand how this below code will be understood and executed.
Need help
var arr = ["a" , "b" , "c" , "d"][1,2,3];
console.log(arr);
What does that arr definition even mean?
Arrays in JS are one dimensional.
So var arr = ["a" , "b" , "c" , "d"][1,2,3];
Here [1,2,3] represents the indexes.
And comma is the least preceded operator so [1,2,3] will return you 3 (i.e. The right most element always).
Therefore
The result will return you the value at index 3 that's d
Update:
Let's see some more points for comma operator:
From MDN:
The comma operator evaluates each of its operands (from left to right)
and returns the value of the last operand.
So, It means:
var a = (10, 18);
a; //18
Also note that it's essential to mention them inside brackets while assigning to a variable.
If you try something like:
var a = 1,2,3;
a; // will return 1
So it's important that you wrap them using ().
When will you use it , operator?
You can use the comma operator when you want to include multiple
expressions in a location that requires a single expression. The most
common usage of this operator is to supply multiple parameters in a
for loop.
for (var i = 0, j = 9; i <= 9; i++, j--)
Now that you've understood about comma operator, Let's see few more examples to test our understanding:
1. var a = (1,3)/(2,1);
a // what will be the o/p
2. var a = ["Stack", "OverFlow","is","Awesome"][1,3,2][2,0]
a // What will be the o/p
3. var a = (true,0,1), b, c;
if(b,c,a) {
console.log("Will it execute?");
}
Your example caught me offguard. Here's another example which illustrates what is going on. Essentially a list of comma separated values evaluates to the value of the last.
if (false, false, true) {
console.log("hello")
}
// --> "hello"

javascript ternary statement equivilance [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
JavaScript: Is “z=(x||y)” same as “z=x?x:y” for non-boolean?
Are the following two lines of code equivalent in javascript?
a = b ? b : c
a = b || c
I want to express: "a should be assigned b if b is truthy, otherwise a should be assigned c"
I expect they would both work exactly the same, but I'm not 100% sure.
Yes. The two are almost exactly identical.
Both will first evaluate b. If it's truthy, it'll return b. Else, it'll return c.
As pointed out by #thesystem, if you have a getter method on b, it'll be called twice for the ternary, but only once for the or statement.
Test it using the following snippet:
var o = {};
o.__defineGetter__("b", function() {
console.log('ran');
return true;
});
var d = o.b || o.not;
console.log('-----');
var d = o.b ? o.b : o.not;
Here's the fiddle: http://jsfiddle.net/bqsey/
Logical operators are typically used with Boolean (logical) values;
when they are, they return a Boolean value. However, the && and ||
operators actually return the value of one of the specified operands,
so if these operators are used with non-Boolean values, they may
return a non-Boolean value.
ref: Logical Operators - MDN

Categories

Resources