Skip to content

Instantly share code, notes, and snippets.

@nurmdrafi
Last active July 9, 2022 11:47
Show Gist options
  • Save nurmdrafi/1e686651fd34729eae8f5c7fb9c28869 to your computer and use it in GitHub Desktop.
Save nurmdrafi/1e686651fd34729eae8f5c7fb9c28869 to your computer and use it in GitHub Desktop.
JavaScript Output TEST

Q: What will be the output of following code and why ?

(function ex(){

  var hello = function(){
  console.log(`I am JavaScript lover`)
};
    function hello(){
  console.log(`I am Java lover`)
};

hello();

})()
Solution I am JavaScript lover

Only function name hoisted like:- var hello // undefined allocate memory then assign function body and execute the function.

Function declarations2 are hoisted over variable declarations3 but not over variable assignments1.


Q: What will be the output of following code and why ?

(function ex(){

    function hello(){
  console.log(`I am JavaScript lover`)
};
    function hello(){
  console.log(`I am Java lover`)
};

hello();

})()
Solution I am Java lover

Function declaration hoisted function full body top of the scope. But here, function execution call stack follows last in last out rules.


Q: Transform the array column to row

Input: [
  [1, 6, 7],
  [9, 4, 3],
  [8, 2, 5],
]

Output: [
  [1, 9, 8],
  [6, 4, 2],
  [7, 3, 5],
]
Solution
let input = [
  [1, 6, 7],
  [9, 4, 3],
  [8, 2, 5],
];
for (let i = 0; i < input.length - 1; i++) {
  let l = i + 1;
  let len = input[i].length - l;
  for (let j = 0; j < len; j++) {
    let swap = input[i][l];
    input[i][l] = input[l][i];
    input[l][i] = swap;
    l++;
  }
}
console.log(input);

Q: What Will be the output of the following code?

var a = 10, b = '20', res;
res = !(a < (b / 5)) && ((b / a) < a);
function nextTopper(n){
return n+'NextTopper'
}
console.log(nextTopper(res));
Solution

trueNextTopper


Q: What Will be the output and why?

var obj = {
  a: 10;
}
function sum2(value){
  return (value + 'NextTopper')
  }
function sum(n){
  return sum2('Next' /n)
}
obj.a == '10' ? console.log(sum(obj.a)) : console.log('error');
Solution

NaNNextTopper Because double qual (==) only check values between two(2) data types.


Q: FIND THE ERROR OF THIS CODE AND EXPLAIN HOW TO SOLVE THAT ERROR?

var exOne = "Bangladesh";
console.log(exOne)
["JavaScript", "Java", "PHP"].map (val => console.log(val))
Solution

We know that, in Java script we don’t need to give semicolon it assigned implicitly. But when we want to get access of an array directly then we need to give a semicolon. If we do this code like this way –

var exOne = "Bangladesh";
console.log(exOne);
["JavaScript", "Java", "PHP"].map (val => console.log(val));

Q: Find the error of this code and explain how to solve that error?

"use strict"

for (var i = 0 ; ; ){
if (i > 10) break;
else{
    ex = i;
    continue;
}
console.log(i)
i++
}
Solution
  1. In Line 7 we are seeing that a new variable ex is assign by a value. But if we notice that there don’t have any reference has kept by this name ex. By using “use strict” js can not declare a variable name ex with var keyword implicitly in the global scope. That’s why it will throw a reference error because there don’t have any reference of variable ex. But if there don’t have any “use strict” then it will work perfectly.

  2. In Line 8 by using continue this loop will stop from their and continue it from the start point. That’s why line 10 to line 11 can not be execute in this code. And it will continue again and again so it will create an infinit loop. Because the condition that will stop this loop is depends on the increament value of i variable . But here i variable will not increase so it will run the again and again.

  "use strict"
  var ex;
  for (var i = 0 ; ; ){
  if (i > 10) break;
  else{
      ex = i;
      continue;
  }
  console.log(i)
  i++
  }

Q: What Will be the output of the following code?

function myFunc(){
return '#NextTopper'
}
const topper = myFunc `hello`
console.log(topper)
Solution #NextTopper

Explanation: যখন কোনো Template literal এর আগে আমরা functionName লিখে দেই, এর মানে হচ্ছে Template literal অংশটা function এ আর্গুমেন্ট আকারে চলে যায়। যেমন এইখানে hello আছে , এর আগে আমরা myFunc দিয়েছে -myFunc হলো funtionName. myFunc hello এটা যখন দেওয়া হয় তখন myFunc ফাংশনটা কল হয়ে যায় , আর hello টা ফাংশনে আর্গুমেন্ট আকারে চলে যায়। এখন আমাদের myFunc ফাংশনটা রিটার্ন করেছে #NextTopper আর আমরা ফাংশনটা topper এর মধ্যে রাখি । তাই আমাদের topper এর মধ্যে #NextTopper চলে আসে । আর আমরা topper কে console.log করি সুতরাং আমাদের অউটপুট #NextTopper. এখন যদি আপনারা hello পেতে চান তাহলে myFunC এ arguments টা

console.log করে দেখতে পারেন ! hello পেয়ে যাবেন।
function myFunc(){
  console.log(arguments);
  return '#NextTopper'
}
const topper  = myFunc `hello`
console.log(topper)

Q: What Will be the output of the following code?

let count = 0;
function immediate() {
  if (count === 0) {
    let count = 1;
    console.log(count);
  }
  console.log(count);
}
Solution 1, 0

Explanation: ২০১৫ সালের আগে জাভাস্ক্রিপ্ট এ শুধু গ্লোবাল স্কোপ এবং ফাংশনাল স্কোপ ভেরিয়্যাবল নিয়ে কাজ করা যেত। ইএস6 আসার পর জাভাস্ক্রিপ্ট খুবই গুরুত্বপূর্ণ ২ টি কিওয়ার্ড আমাদের মাঝে নিয়ে আসে। যা হল let এবং const । যার মাধ্যমে আমরা ব্লক স্কোপ ভেরিয়্যাবল নিয়ে কাজ করার সুবিধা পাই। আমাদের প্রোগ্রাম টিতে ২ টি count ভেরিয়্যাবল আছে। ২ টি ভেরিয়্যাবল ই let দ্বারা ডিক্লেয়ার করা হয়েছে। একটি if ব্লক এর ভিতর আর একটি immediate ফাংশনের বাহিরে। এখন if ব্লক এর ভিতর console.log(count) 1 প্রিন্ট করবে। এইখানে জাভাস্ক্রিপ্ট স্বজনপ্রীতি দেখিয়েছে। সে if ব্লক এর ভিতরের count কে প্রিন্ট করেছে। এইখানে মনে রাখতে হবে জাভাস্ক্রিপ্ট ব্লক স্কপ ভেরিয়্যাবলকে ওই ব্লকে বেশি প্রাধান্য দিবে। তেমনি দ্বিতীয় console.log(count) 0 প্রিন্ট করবে। কারন if এর ভিতরের count ওই ব্লক ছাড়া অন্য কোথাও থেকে অ্যাক্সেস করা যাবে না। তাই দ্বিতীয় console.log(count) টি 0 প্রিন্ট করবে।


Q: What Will be the output of the following code?

const myLanguage = "JavaScript"
const allLanguage = [];
function sayMyLanguage (myLanguage, allLanguage){
    myLanguage = "Java";
    allLanguage.push(myLanguage)
    console.log(`I am a ${myLanguage} lover`);
}

sayMyLanguage(myLanguage, allLanguage)
console.log(myLanguage);
console.log(allLanguage);
Solution First will print 'Javascript and empty array. Second will print 'I am a Java lover'

Q: What Will be the output of the following code?

var y = 1;
if (function f() {}){
    y += typeof f;
}
console.log(y);
Solution "1undefined"

Explanation: প্রথমে আমরা জানবো if কিভাবে কাজ করে? if(condition)

  • এখানে condition টা এমন একটা expression হবে যে কিনা truthy or falsy হিসেবে বিবেচিত হবে।
  • আমরা এইখানে কি করেছি একটা expression এর যায়গায় function declare করেছি। ঝামেলা টা এইখানেই, আমরা যখন expression এর যায়গায় function declaration করতেছি তখন এই ফাংশন টা function declaration এর মত আচরণ করবে না।
  • যেহেতু function declaration হচ্ছে না। তাই এইটা execution context এর creation phase এ function টার রেফারেন্স রাখতে পারবে না।

কিন্তু আপনি যদি জিনিস টা এইভাবে লিখতেন। if( f = function () {}) তাহলে typeof f, function হবে। সুতরাং, আমরা y += typeof f থেকে পাচ্ছি "1undefined"


Q: What will be the output of the following code and why?

let a = 5, b = '2';
let A = a + +b;
console.log(A++);
console.log(--A);
Solution 7, 7

Explanation: Javascript has bug like 2+"2"=4 but it never should be,,, here A = a+ +b it working like 5+ +"2" =5+space+"2" ...here space is escaped and the equation becomes 5+"2" =7 ,,, It's a bug on javascript Language... however A becomes 7. okay let's finish it, A=7 A++ =7+1 But still A++ console value will be =7,,because it's Postfix Then New A console=8 --A = 8-1 But this time --A result will be assign in the console.. Cuz It's Prefix That means.. --A=7. Final output comes. A=7 Console.log (A++)=7 Console.log (--A)=7.


Q: What will be the output of the following code and why?

var LANGUAGE = "Java";
var language = "JavaScript";

function getLanguage() {
  if (!language) {
    var language = LANGUAGE;
  }
  return language;
}
console.log(`I love ${getLanguage()}`);
Solution

I love Java

  • var assign 'undefined' top of the scope
  • let assign 'undefined' where variable is declared

Explanation: In JavaScript when we declare a variable by using var keyword, It hoisted to top of the scope and var assign 'undefined' to its identifier like here, language = 'undefined'. In getLanguage() function we use if statement and checking !language, here NOT(!) are checking falsy values. We JavaScript has 5 falsy values:- 0, null, NaN, empty string ("" or '' or ``) and undefined. So condition matched changes values JavaScript to Java and returned it. Output shows => I love Java.

If we use let instead of var then the output could be I love JavaScript, because let also hoisted to top of the scope but assign undefined where variable is declared and replace instantly with value. So !language condition is false here, did not pass into if statement and returns unchanged language. Output shows => I love JavaScript.


Q: What will be the output and why?

function printMe(n) {
  switch (n) {
    case n == 1:
      console.log("This is One");
      break;
    case n == 2 :
      console.log("This is Two");
      break;
    case n > 2 && n < 5:
      console.log("Greater than two and less than 10");
      break;
    default:
      console.log("No match found");
  }
}
printMe(2); // "No match found"
printMe(5); // "No match found"
Solution

Explanation: Switch statements always compare values strictly. If we want to use conditions in case we have to use switch (true) instead of switch (n).

  function printMe(n) {
  switch (true) {
    case n == 1:
      console.log("This is One");
      break;
    case n == 2:
      console.log("This is Two");
      break;
    case n > 2 && n < 5:
      console.log("Greater than two and less than 10");
      break;
    default:
      console.log("No match found");
  }
}
printMe(2); // "This is Two"
printMe(5); // "Greater than two and less than 10"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment