Test your JavaScript Skills:Fundamentals-20

Part 20

Introduction

Namaste, In this blog I will discuss 7 code scenarios on Javascript fundamentals.

  1. Identify the output of the below program

     if (10-10 || 20-20)
       console.log("true");
     else
       console.log("false");
    

    a.true

    b.false

    c.no output

    d.error

    e.undefined

    Answer: b

    Reason: The computation of both operations yields 0 and 0 is a falsey value.

  2. Identify the output of the below program

     let a=20;
     if ((a=a+5) || (a=a+5)) 
       console.log("true", a);
     else
       console.log("false",a);
    

    a.true 30

    b.false 30

    c.true 25

    d.false 25

    e.error

    Answer: c

    Reason: The first conditional expression is true hence the second expression is not evaluated.

  3. Identify the output of the below program

     let res = NaN || undefined || 0 || null  ; 
     conseolog("value",res);
    

    a.NaN

    b.undefined

    c.0

    d.null

    e.error

    Answer:d

    Reason: Since none of the values are truthy the last value is taken which is null.

  4. Identify the output of the below program

       let b=20;
       if ((b=b-20) || (b=b+5)) 
         console.log("true", b); 
       else
         console.log("false",b);
    

    a.true 0

    b.true 5

    c.false 0

    d.false 5

    e.false 26

    Answer:b

    Reason: The first condition is falsey hence second is evaluated which is true therefore the computation result is 5 which is truthy value.

  5. Identify the output of the below program

     if (10-10 || 20-21)
       console.log("true"); 
     else
       console.log("false");
    

    a.false

    b.undefined

    c.error

    d.true

    e.no output

    Answer:d

    Reason: The -1 is a truthy value.

  6. Identify the output of the below program

     if (console.log("if"))
           var i=10;
    

    a.if

    b.10

    c.no-output

    d.error

    e.undefined

    Answer: The value 'if' is printed.

    Reason: console.log() is executed.

  7. Identify the output of the below program

      if (console.log("if1") && console.log("if2")) 
             var i=10;
      else
             var j=11;
    

    a.if1

    b.if2

    c.if1,if2

    d.error

    e.11

    Answer: a

    Reason: Only 'if1' is printed as the condition is falsey because 1st console.log() returns undefined.

Did you find this article valuable?

Support Programming with Mahavir by becoming a sponsor. Any amount is appreciated!