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?

Jack Bashford
  • 38,499
  • 10
  • 36
  • 67
Drup
  • 69
  • 1
  • 5
  • You might want to check this question for declaring multiple variable. https://stackoverflow.com/questions/694102/declaring-multiple-variables-in-javascript – Gelxxxx Jul 22 '19 at 05:03
  • 1
    If you want to declare multiple , use `=` not `,` `var jhon = kate = " kate";` – hs-dev2 MR Jul 22 '19 at 05:04
  • @hs-dev2MR That's wrong. – melpomene Jul 22 '19 at 05:05
  • Why @melpomene Explain me , I will appreciate :> – hs-dev2 MR Jul 22 '19 at 05:07
  • @hs-dev2MR Try it under `'use strict'`. It only declares `jhon`, not `kate`. – melpomene Jul 22 '19 at 05:08
  • @hs-dev2MR That'll make `jhon` a copy of `kate`, and `kate` will be assigned the value `" kate"`. Different to declaring multiple independent variables in the same statement. – Jack Bashford Jul 22 '19 at 05:09
  • @Jack It won't make anything a copy of anything. It will assign the *same value* to both variables. Since strings are immutable, that's fine. If you'd assign a mutable value like an object, and you then mutate that object, the mutation will show on both `jhon` and `kate`, which may or may not be what you expect. – deceze Jul 22 '19 at 05:11
  • I got it !!! If I declare like `var j = k = {a:11};` and modify `k.a = 99` => `j.a` will also modify to `99` Right :> Thank – hs-dev2 MR Jul 22 '19 at 05:11
  • Oh, right - so when the value is an object, it'll be the same reference passed to both variables, but with a primitive, the same value is passed? – Jack Bashford Jul 22 '19 at 05:12
  • @hs-dev2MR The main problem is that `k` is a global variable there (or an error under `'use strict'`). – melpomene Jul 22 '19 at 05:12
  • @Jack Well, *always* the same value is assigned; just with objects, that value is a reference to an object. – deceze Jul 22 '19 at 05:12
  • Okay, my bad - thanks @deceze. – Jack Bashford Jul 22 '19 at 05:14

4 Answers4

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.

Jack Bashford
  • 38,499
  • 10
  • 36
  • 67
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

melpomene
  • 79,257
  • 6
  • 70
  • 127
Dng
  • 91
  • 6
-1

what you want is

var jhon = kate = " kate";
console.log(jhon + kate);
var jhon = "jhon ", kate = " kate";
console.log(jhon + kate);
kcsujeet
  • 426
  • 2
  • 8
  • `var jhon = kate = "kate";` is wrong because it only declares `jhon`, not `kate`. – melpomene Jul 22 '19 at 05:15
  • got it. but since, what the question is dealing with string and not with arrays or objects, this is also correct right??? – kcsujeet Jul 22 '19 at 05:25
  • Do you know what `var` does? – melpomene Jul 22 '19 at 05:29
  • detonates a bomb? – kcsujeet Jul 22 '19 at 05:37
  • No, it declares a variable. Please refer to [MDN `var`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var). In particular, `var x = y` declares `x`, but not `y`, whereas `var x, y` declares both `x` and `y`. – melpomene Jul 22 '19 at 05:39
  • See also this [live demo](https://tio.run/##y0osSyxOLsosKNHNy09J/f9fvbQ4VaG4pCgzuUTdmourLLFIoULBVqESiNUzUnNy8tWt//8HAA). – melpomene Jul 22 '19 at 05:44
  • if i do var x=y='a', y is a global variable. i don't see anything wrong in declaring a global variable here in this case. – kcsujeet Jul 22 '19 at 05:49
-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
Mayank Dudakiya
  • 2,310
  • 21
  • 25
  • `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. – melpomene Jul 22 '19 at 05:14