Created
December 14, 2016 04:53
-
-
Save anonymous/f14bab252912e025b4d0c67bb9dfdfba to your computer and use it in GitHub Desktop.
JS Bin Type Conversion JavaScript Test Question // source http://jsbin.com/bopiji
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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