Explain the output of this code !!!!! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 14

Explain the output of this code !!!!!

var num1 = [1,2] ; var num2 = [3,4] ; var set = num1 + num2 ; console.log(set.length); Output : 6 Why output is 6 ?? How it works ???

31st Mar 2018, 6:28 AM
Arushi Singhania
Arushi Singhania - avatar
28 Answers
+ 9
This happen because "+" sign works for numbers and strings. And here "+" automatically convert array to string: "1,2"+"3,4" = "1,23,4". So "1,23,4" as string has length 6.
31st Mar 2018, 6:36 AM
Vladi Petrov
Vladi Petrov - avatar
+ 8
What is the output of the following code? let nums = [3, 4, 5]; let all = [1, 2, ...nums, 6]; console.log(all[3]); answer 4
10th Feb 2020, 8:35 AM
Pranjal Singh
Pranjal Singh - avatar
+ 5
hangout..? i dont use it....
31st Mar 2018, 11:38 AM
STRIKER
+ 4
I am also done.
31st Mar 2018, 7:12 AM
Arushi Singhania
Arushi Singhania - avatar
+ 4
Parisa Baastani Console.log output any statement in java script. Document.write display it in html document but console.log displays it directly in console.
1st Apr 2018, 8:15 AM
Arushi Singhania
Arushi Singhania - avatar
+ 3
Vladi Petrov Thank you so much !!!!
31st Mar 2018, 6:38 AM
Arushi Singhania
Arushi Singhania - avatar
+ 3
im done.....
31st Mar 2018, 7:11 AM
STRIKER
+ 3
Vladi Petrov Thank you so much. I would like to ask a question. Would you like to join our programming group ???
31st Mar 2018, 7:13 AM
Arushi Singhania
Arushi Singhania - avatar
+ 3
Ok
31st Mar 2018, 11:44 AM
Arushi Singhania
Arushi Singhania - avatar
+ 3
i dont know about cinsole.log, anybody here to explain about it?
1st Apr 2018, 8:07 AM
Parisa Baastani
Parisa Baastani - avatar
+ 3
Hi, 'var' datatype behaves as what type of data is initialized that's why initialization is necessary while declaring 'var' variable. '+' sign is considered as concatenate operator when used with string values. Since right side is string then 'var' will behave like a string. Length function returns the length of a string that's why answer is 6. Thanks, Jai Patel
1st Apr 2018, 9:51 AM
Jai Verma
Jai Verma - avatar
+ 3
What is the output of this code? function letItBe() { let v = 2; if (true) { let v = 4; console.log(v); } console.log(v); } letItBe(); Ans: 4 2
23rd May 2020, 7:20 PM
Pramod Vishwakarma
Pramod Vishwakarma - avatar
+ 2
S. Kaouche Nope It isn't because of .length method
1st Apr 2018, 8:13 AM
Arushi Singhania
Arushi Singhania - avatar
+ 1
[3] means, the fourth ---> 4
10th Jun 2020, 6:51 AM
Rusz Tamás
Rusz Tamás - avatar
0
ya that what i was thinking...
31st Mar 2018, 6:45 AM
Abhishek Rawat
Abhishek Rawat - avatar
0
if you are counting "commas(,)", then also length will be 5. Why are you taking 23 separately? they are single as number twenty three(according to what output you showed)
31st Mar 2018, 6:55 AM
Abhishek Rawat
Abhishek Rawat - avatar
0
ya.. shit now i got that.. i just forgot nd got confused.. sorry Vladi
31st Mar 2018, 7:03 AM
Abhishek Rawat
Abhishek Rawat - avatar
0
Isn't it because of the .length method which add 0 position to each array ? So it becomes 0,1,2 (num1)+ 0,3,4 (num2)= 6 The 0 position index is included in the .length method , isn't it ?
31st Mar 2018, 5:49 PM
S. Kaouche
S. Kaouche - avatar
0
you can add two arrays in JS using the concat method. var v = a.concat(arr); console.log(v)
9th Apr 2018, 5:43 AM
Sujoy Datta
Sujoy Datta - avatar
0
What is the output of the following code? let nums = [3, 4, 5]; let all = [1, 2, ...nums, 6]; console.log(all[3]); answer 4
24th Dec 2019, 9:26 AM
Rana Abid
Rana Abid - avatar