Table of contents
Introduction
Namaste, In this blog I will discuss 10 code scenarios on Javascript .
Identify the output of the below program
let str = "Namaste Java"; let str2 = str.replaceAll("Java", "Javascript"); console.log(str2);
a.Namaste Java
b.Namaste Javascript
c.undefined
d.null
e.error
Answer:b
Reason:The string Java is replaced by Javascript.
Identify the output of the below program
let a=true; a &&= false; console.log(a);
a.true
b.undefined
c.null
d.false
e.error
Answer:d
Reason:The logical and assignment operator evaluates to false in the above scenario.
Identify the output of the below program
let num= 1_0_0_0; console.log(num);
a.1
b.10
c.1000
d.100
e.error
Answer:c
Reason:Numeric separator enhances code readability.
Identify the output of the below program
let a=false; a ||= true; console.log(a);
a.false
b.true
c.undefined
d.error
e.null
Answer:b
Reason:The expression evaluates to true.
Identify the output of the below program
let a=true; a ||= false; console.log(a);
a.true
b.false
c.undefined
d.null
e.error
Answer:a
Reason:The expression evaluates to true.
Identify the output of the below program
let a=false; a &&= false; console.log(a);
a.true
b.false
c.error
d.undefined
e.error
Answer:b
Reason:The expression evaluates to false.
Identify the output of the below program
let str = "Java Java"; console.log(str.replaceAll("Java", "Javascript"));
a.Javascript Javascript
b.Java Java
c.Javascript
d.Java
e.error
Answer: a
Reason: The string Java is replaced by Javascript at all places.
Identify the output of the below program
let x; let y = 10; x ??= y; console.log(x);
a.undefined
b.null
c.10
d.true
e.false
Answer:c
Reason:Since x is undefined value 10 is assigned to it.
Identify the output of the below program
let x=2; let y = 10; x ??= y; console.log(x);
a.2
b.10
c.false
d.true
e.error
Answer:a
Reason:Since x is already initialized the value of y is not initialized.
Identify the output of the below program
let str = "Namaste Java"; let str2 = str.replaceAll("Java", "Javascript"); console.log(str);
a.Namaste Java
b.Namaste Javascript
c.error
d.null
e.undefined
Answer:a
Reason:The host string is not updated by replaceAll() function.