Skip to content

Instantly share code, notes, and snippets.

@newbreedofgeek
Forked from anonymous/index.html
Created December 14, 2016 04:54
Show Gist options
  • Save newbreedofgeek/72558a458d2c8cb4c09aa7b5bba0e440 to your computer and use it in GitHub Desktop.
Save newbreedofgeek/72558a458d2c8cb4c09aa7b5bba0e440 to your computer and use it in GitHub Desktop.
JS Bin Type Conversion JavaScript Test Question // source http://jsbin.com/bopiji
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="Type Conversion JavaScript Test Question">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
// Question:
// -----------------------------------------
// What is the value of a after below script execution?
var x = "1";
var y = 1;
a = x + y;
/* is it?
A. "11" (string)
B. 11 (number)
C. 2 (number)
D. "2" (string)
*/
// Answer:
// -----------------------------------------
console.log(a);
console.log(typeof a);
/*
The answer is "11" (string).
Why? In JavSctipt type conversion is handled implicitly by the engine which uses "best guess rules" for type casting. In this e.g "x" is a String and "y" is a Int and the engine knows that, when we put the plus sign between them, the engine has to make a guess on what you are trying to do. Because we use "+" for string concatenation it assumes we are working with strings and converts "y" to a string as well. If you replace the "+" with "-" then the engine assumes they are numbers and "-" has no operation on strings.
*/
</script>
<script id="jsbin-source-javascript" type="text/javascript">// Question:
// -----------------------------------------
// What is the value of a after below script execution?
var x = "1";
var y = 1;
a = x + y;
/* is it?
A. "11" (string)
B. 11 (number)
C. 2 (number)
D. "2" (string)
*/
// Answer:
// -----------------------------------------
console.log(a);
console.log(typeof a);
/*
The answer is "11" (string).
Why? In JavSctipt type conversion is handled implicitly by the engine which uses "best guess rules" for type casting. In this e.g "x" is a String and "y" is a Int and the engine knows that, when we put the plus sign between them, the engine has to make a guess on what you are trying to do. Because we use "+" for string concatenation it assumes we are working with strings and converts "y" to a string as well. If you replace the "+" with "-" then the engine assumes they are numbers and "-" has no operation on strings.
*/
</script></body>
</html>
// Question:
// -----------------------------------------
// What is the value of a after below script execution?
var x = "1";
var y = 1;
a = x + y;
/* is it?
A. "11" (string)
B. 11 (number)
C. 2 (number)
D. "2" (string)
*/
// Answer:
// -----------------------------------------
console.log(a);
console.log(typeof a);
/*
The answer is "11" (string).
Why? In JavSctipt type conversion is handled implicitly by the engine which uses "best guess rules" for type casting. In this e.g "x" is a String and "y" is a Int and the engine knows that, when we put the plus sign between them, the engine has to make a guess on what you are trying to do. Because we use "+" for string concatenation it assumes we are working with strings and converts "y" to a string as well. If you replace the "+" with "-" then the engine assumes they are numbers and "-" has no operation on strings.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment