0

Some questions on data types

1) [10]===10 => false (ok, I get this one) 2) var a = [10]; a[0] == 10 => true (obvious) 3) [10]==10 => true (yeah... so JS does not care about data types, huh?) on 3): to me this means Array == Number. Why is JS so sloppy about data types? Well, I can declare a variable, e.g a=42, and then reassign it with a="blub". JS doesn't mind. Sometimes that is a good thing, but on the other hand in some cases it is very hard for me to guess, what a code or variable represents. See example 3. TBC...

6th Jul 2017, 9:38 PM
Pete Wright
Pete Wright - avatar
4 Answers
+ 1
Answer 1. ------------ [10] == 10 will be true Because by using == you're saying, 'I do not give a damn about their types, just check if their values are same somehow' So, in these cases, you need to use === if you want to check the datatype also. So, "10" === 10 // will return false. Answer 2. ------------ To check if a variable 'x' is array use, Array.isArray(x); Answer 3. ------------ 2 * "2" + "2" + 2 JS interpreter will read it from left to right. And, * operator has higher precedence than the + operator So, at first, it'll check 2*"2", it'll ask itself, "does multiplying an integer and a string make any sense?" "No!, the developer must have made a minor mistake, let me fix that for him." so, it'll convert the string "2" to int 2, and return: 4 // integer // Next, it'll try to do, 4+ "2" + 2, now, it'll think, "Does adding an integer and a string make any sense?" "Hmm..., + is used to add integers and also to concatenate strings" "Developer must have meant to concatenate them, not sum them, let me ignore these silly mistakes" So, it'll convert everything to string and return "422" // string // Just remember these and you'll be alright "2" / 2 // 1 "2" - 2 // 0 "2" * 2 // 4 "2" % 2 // 0 "2" + 2 // "22"
6th Jul 2017, 11:49 PM
Salekin
Salekin - avatar
+ 1
@Pete Wright: Sorry for late answer. There is a concept 'Operator Overloading' doing it's magic here. That means one operator doing multiple functions. You can read about this here, https://en.wikipedia.org/wiki/Operator_overloading 2 * "2" (how js interpreter thinks) 1. Can I multiply an int with a string? => no, go to step 2. 2. What if I convert int 2 to string '2', then can I multiply 2 strings? => no go to step 3. 3. What if I convert string '2' to int 2, then can i multiply 2 ints? => Yes, return 2*2 = 4 2 + "2" (how js interpreter thinks) 1. Can I add an int with a string? => no, go to step 2. 2. What if I convert int 2 to string '2', then can I add 2 strings? => Yes, return '2' + '2' = '22' This is just how the language was written, don't worry about these stuff. In ruby you can multiply a string with an int. So, "Hello" * 2 returns "HelloHello"
10th Jul 2017, 6:22 PM
Salekin
Salekin - avatar
0
Another question is: How can I check if a variable is an Array? Since typeof([42]) returns "Object" I don't know what that object stands for. Is there a function or "hack" that gives me the right answer? One more thing: 2*"2"+"2"+2 = 422 (again, JS mixes up data types) I never know when something will be treated as a String and when as a Number. Can someone explain that to me?
6th Jul 2017, 9:40 PM
Pete Wright
Pete Wright - avatar
0
Thank you for the explanation. So in: 2 * "2" the interpreter thinks the string must have been meant a number and converts it. Why does the interpreter think of a string in the second case then: 4 + "2" ? Following your first explanation the interpreter should convert this string to a number too. I am confused. When will I get the string and when the number? Damn you, JavaScript! :(
8th Jul 2017, 10:53 PM
Pete Wright
Pete Wright - avatar