What is the output of this code? Would anybody help me solve out? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

What is the output of this code? Would anybody help me solve out?

What is the output of this code? let a = [1,3,5][2] ; let b = [x:4, y:10].y ; let c = ["199"][0] ; console.log(a+b+c);

17th Dec 2022, 10:58 AM
Sony
Sony - avatar
6 Answers
+ 6
First look at Tibor Santa's correction note, <b> should be an object if we want to access its member by member-access operator (dot) Output can only be 151 when <c> was declared as string, rather than an array. [ "199" ] [ 0 ] yields "199", the first array element "199" [ 0 ] yields "1" the first character (Edit) [ "199" ] [ 0 ] [ 0 ] also yields the first character from the first array element You might be questioning the correctness of the code now that you see this ...
17th Dec 2022, 2:16 PM
Ipang
+ 6
Syntax error. Your variable b is invalid. If you fix it to be a javascript object (change to curly braces): let b = {x: 4, y: 10}.y; Then the result is: 5 + 10 + "199" = 15199
17th Dec 2022, 11:07 AM
Tibor Santa
Tibor Santa - avatar
+ 3
Sony Hossain You can always see the output of any code by running it. That's what Code Playground is for.
17th Dec 2022, 1:10 PM
Emerson Prado
Emerson Prado - avatar
+ 2
Sony Hossain I see. Get used to be specific in the questions. When you need to understand how the code works, ask that. Don't leave that for the readers guesses. Ah, always include a tag with the language name.
17th Dec 2022, 2:36 PM
Emerson Prado
Emerson Prado - avatar
+ 2
Thank you for your reply.. well I always use tags.. but this time I forgot to include the language name.. it might be a fault of mine.. next time I'd try to do so.. thanks! Emerson Prado
17th Dec 2022, 2:42 PM
Sony
Sony - avatar
+ 1
Thanks for your reply... Yeah I've already tried it in the playground.. it's 151 but how it comes up with this result.. wanted to know in details from sololearn experts.. thank you. Emerson Prado
17th Dec 2022, 2:00 PM
Sony
Sony - avatar