0

I am learning JavaScript basics. While doing multiple variable declaration in a single statement, getting different results.

var jhon, kate = " kate";
console.log(jhon + kate);
var jhon = "jhon ", kate = " kate";
console.log(jhon + kate);

Question : Why first console.log prints value for kate not for jhon?

12
  • You might want to check this question for declaring multiple variable. stackoverflow.com/questions/694102/… Commented Jul 22, 2019 at 5:03
  • 1
    If you want to declare multiple , use = not , var jhon = kate = " kate"; Commented Jul 22, 2019 at 5:04
  • @hs-dev2MR That's wrong. Commented Jul 22, 2019 at 5:05
  • Why @melpomene Explain me , I will appreciate :> Commented Jul 22, 2019 at 5:07
  • @hs-dev2MR Try it under 'use strict'. It only declares jhon, not kate. Commented Jul 22, 2019 at 5:08

4 Answers 4

4

Because doing this:

var jhon, kate = " kate";

Is equivalent to:

var jhon;
var kate = " kate";

Which is:

var jhon = undefined;
var kate = " kate";

Which, when concatenated, gives:

undefined kate

You simply haven't given jhon a value in the first example.

Sign up to request clarification or add additional context in comments.

Comments

0

var jhon,kate = " kate";

You defined 2 variables, but you only set value for kate. Due to the jhon is not set value, when you console.log it, it will be print undefined

Comments

-1

what you want is

var jhon = kate = " kate";
console.log(jhon + kate);
var jhon = "jhon ", kate = " kate";
console.log(jhon + kate);

7 Comments

var jhon = kate = "kate"; is wrong because it only declares jhon, not kate.
got it. but since, what the question is dealing with string and not with arrays or objects, this is also correct right???
Do you know what var does?
detonates a bomb?
No, it declares a variable. Please refer to MDN var. In particular, var x = y declares x, but not y, whereas var x, y declares both x and y.
|
-1

You should use = to assign a value to the variable not ,

var jhon = kate = "kate";
console.log(jhon + kate);

// Output will be : kate kate

Following approach is easily solve your issue which very easy to handle.

let
  jhon = 'kate',
  kate = 'kate',
  cena = 'foobar'
;

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

For following a will equal 10 and b will equal 20.

var a, b;
[a, b] = [10, 20];

console.log(a);
// expected output: 10

console.log(b);
// expected output: 20

1 Comment

var jhon = kate = "kate"; is wrong because it only declares jhon, not kate. Destructuring assignment is irrelevant. If you want to assign different values, separate statements are better: a = 10; b = 20;. If you want to assign the same value, use a = b = 42. But you can't combine that with a declaration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.