Skip to content

aviyehuda.com

Menu
  • Open Source
  • Android
  • Java
  • Others
  • Contact Me
  • About Me
Menu

4 JavaScript trivia questions that may help you understand the language a bit better

Posted on 21/03/2012

The next 4 Javascript questions may seem easy to some of you, but I am sure that more than a few will get confused.
I believe that these 4 simple questions will help you understand a key feature of the language.

Question: What will happen after executing the next JS code?

function outer(){
	var x=1;
	var inner = function(){
		alert(">"+x);
	}

	x=2;
	return inner;
}

outer();

1. Nothing
2. alert ‘>undefined’
3. alert ‘>1’
4. alert ‘>2’

Answer:
Nothing, because outer() returned a function but it wasn’t executed.

Question: What will happen after executing the next JS code?

function outer(){
	var x=1;
	var inner = function(){
		alert(">"+x);
	}
	x=2;
	return inner;
}

outer()();

1. Nothing
2. alert ‘>undefined’
3. alert ‘>1’
4. alert ‘>2’

Answer:
alert ‘>2’, because the x that inner() uses is the same x the outer() uses.

Question: What will happen after executing the next JS code?

function outer(){
	var x=1;
	var inner = function(x){
		alert(">"+x);
	}
	x=2;
	return inner;
}

outer()();

1. Nothing
2. alert ‘>undefined’
3. alert ‘>1’
4. alert ‘>2’

Answer:
alert ‘>undefined’. Inner uses a variable x which is different than the outer x. The inner x is never initialized

Question: What will happen after executing the next JS code?

function outer(){
	var x=1;
	var inner = function(x){
		alert(">"+x);
	}
	x=2;
	return inner;
}

outer()(3);

By now the answer should be obvious: alert ‘>3’ .

1 thought on “4 JavaScript trivia questions that may help you understand the language a bit better”

  1. Roy says:
    11/06/2012 at 08:10

    Great refreshment

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *


About Me

REFCARD – Code Gems for Android Developers

Categories

  • Android
  • AWS
  • AWS EMR
  • bluetooth
  • Chrome extension
  • ClientSide
  • Clover
  • Coding Coventions
  • Data Lake
  • General
  • GreaseMonkey
  • Hacks
  • hibernate
  • hibernate validator
  • HTML5
  • HtmlUnit
  • Image Manipulation
  • Java
  • Java Technologies
  • JavaScript
  • Java_Mail
  • JEE/Network
  • Job searching
  • Open Source
  • Pivot
  • projects
  • Pure Java
  • software
  • Spark
  • Trivia
  • Web development

Archives

  • March 2022 (1)
  • January 2022 (1)
  • January 2021 (1)
  • December 2018 (1)
  • August 2018 (1)
  • October 2013 (1)
  • March 2013 (1)
  • January 2013 (2)
  • July 2012 (1)
  • April 2012 (1)
  • March 2012 (1)
  • December 2011 (1)
  • July 2011 (1)
  • June 2011 (1)
  • May 2011 (2)
  • January 2011 (1)
  • December 2010 (1)
  • November 2010 (3)
  • October 2010 (4)
  • July 2010 (1)
  • April 2010 (2)
  • March 2010 (1)
  • February 2010 (2)
  • January 2010 (5)
  • December 2009 (10)
  • September 2009 (1)
 RSS Feed
1d96f52e7159fe09c7a3dd2a9816d166-332
©2023 aviyehuda.com | Design: Newspaperly WordPress Theme