Asked by:
what is the precedence of datatype while comparison .

Question
-
User2102072086 posted
hi
when I compair in javascript like so , then it converts one type to another type. which is based on precedence. can I get link of that precedence.
if ("2"<3)
{
}
Sunday, June 28, 2020 5:51 PM
All replies
-
User1535942433 posted
Hi rajemessage,
As far as I think,the precedence of datatype from highest to lowest is user-defined data types (highest)>sql_variant>xml>datetimeoffset>datetime2>datetime>smalldatetime>date>time>float>real>decimal>money>smallmoney>bigint>int>smallint>tinyint>bit>ntext>text>image>timestamp>timestamp>nvarchar (including nvarchar(max) )>nchar>varchar (including varchar(max) )>char>varbinary (including varbinary(max) >binary (lowest).
More details,you could refer to below article:
Best regards,
Yijing Sun
Monday, June 29, 2020 2:16 AM -
User2102072086 posted
question is for javascript not for tsql
Monday, June 29, 2020 7:45 AM -
User1535942433 posted
Hi rajemessage,
when I compair in javascript like so , then it converts one type to another type. which is based on precedence. can I get link of that precedence.Accroding to your description,you have converted one type to another type,which is based on the size of the type instead of precedence.
Implicit Casting is converting a smaller type to a larger type size and Explicit Casting is converting a larger type to a smaller size type.
About the size of datatype,you could refer to below article:
https://www.w3schools.com/cs/cs_type_casting.asp
Besides,the operator have the precedence,you could refer to below article:
https://docs.w3cub.com/javascript/operators/operator_precedence/
Best regards,
Yijing Sun
Monday, June 29, 2020 9:30 AM -
User-474980206 posted
Here is a good explanation
https://2ality.com/2019/10/type-coercion.html
but in your sample
a < 3
if a is a string it will try to convert to number, if it can, then numeric compare, if it can not then string compare
"1" < 3 // numeric compare - true
"a" < 3 // string compare - false
"\t" < 3 // string compare - trueso in general javascript does the right thing.
note: the switch does a "===", match value and type. this was probably a design flaw, but too late to fix.
switch ("1") {
case 1:
console.log("match"); // no
break;
case "1":
console.log("match"); // yes
break;
}Monday, June 29, 2020 2:55 PM -
User2102072086 posted
the link is of c# and i am talking about javascript. thank u.
Tuesday, June 30, 2020 7:54 AM -
User1535942433 posted
Hi rajemessage,
As far as I think,javascript data types could mutual convert.If you need to convert to string type,you could add .tostring().Also,you could convert other type.Most of the time, operators and functions automatically convert the values given to them to the right type.When the left and right value have differernt type,the comparison have errors.
Just like:
<script> function compare() { var x = true; var y = Number(x); if (y < 3) { alert(y) }; var z = 1; var e = Boolean(z); if (e < 3) { alert(3) } else { alert(e) }; var n = "2"; var m = parseInt(n); if (m < 3) { alert(m) }; } </script>
So,I don't know what your meaings of the precedence of datatype while comparison.Could you tell us more details of your reqiurment?
More details,you could refer to below article:
https://javascript.info/type-conversions
Best regards,
Yijing Sun
Wednesday, July 1, 2020 6:27 AM -
User753101303 posted
Hi,
Try https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Equality#Description which points to a spec (11.9.3).
Wednesday, July 1, 2020 2:58 PM -
User2102072086 posted
https://developer.mozilla.org/en-US/docs/Glossary/Type_coercion
Sunday, July 5, 2020 9:56 AM -
User1535942433 posted
Hi rajemessage,
JavaScript is loosely typed language and most of the time operators automatically convert a value to the right type but there are also cases when we need to explicitly do type conversions.There are two most common data conversions :
1.Converting Values to String
2.Converting Values to Numbers
Implicit Conversion:
There are various operator and functions in JavaScript which automatically converts a value to the right type like alert() function in JavaScript accepts any value and convert it into a string.
explicit Conversion:
Converting Values to Strings:
String() or toString() function can be used in JavaScript to convert a value to a string.
Converting Values to Numbers:
We can use Number() function in JavaScript to convert a value to a Number. It can convert any numerical text and boolean value to a Number. In case of strings of non-numbers it will convert it to a NaN(Not a Number).
Best regards,
Yijing Sun
Monday, July 6, 2020 7:21 AM