a is of type Object, while b is of type string. In this case a and b are not the same type. I thought strings behave like value types? Well, it depends who you ask. Now it becomes interesting: var a = "12" "3" Īlert(a = b) // returns true, because strings behave like value typesīut how about this?: var a = new String("123") Īlert(a = b) // returns false !! (but they are equal and of the same type) Strings are not value types, but in Javascript they behave like value types, so they will be "equal" when the characters in the string are the same and when they are of the same length (as explained in the third rule) There's more to the story than being equal and being of the same type.Ī = b returns true if a and b have the same value and are of the same typeĪ = b returns true if a and b reference the exact same objectĪ = b returns true if a and b are both strings and contain the exact same characters Var ab_eq = (a = b) // false (even though a and b are the same type) So, let's take the following code: var a = It actually means that both operands reference the same object, or in case of value types, have the same value. Some will say that = means equal and of the same type, but that's not really true. In the answers here, I didn't read anything about what equal means. My advice is to bypass the question entirely and just don't use the String constructor to create string objects from string literals. Which one is correct? That really depends on what you're trying to compare. Here the = operator is checking the values of the two objects and returning true, but the = is seeing that they're not the same type and returning false. For example, consider the comparison of a string primitive with a string object created using the String constructor. The special case is when you compare a primitive with an object that evaluates to the same primitive, due to its toString or valueOf method. For objects, = and = act consistently with one another (except in a special case). All of the comparisons just shown produce false with the = operator.Ī good point was brought up by in the comments and in Laybaert's answer concerning objects. My advice is to never use the evil twins. These are some of the interesting cases: '' = '0' // false The rules by which they do that are complicated and unmemorable. The evil twins do the right thing when the operands are of the same type, but if they are of different types, they attempt to coerce the values. If the two operands are of the same type and have the same value, then = produces true and != produces false. The good ones work the way you would expect. JavaScript has two sets of equality operators: = and !=, and their evil twins = and !=. To quote Douglas Crockford's excellent JavaScript: The Good Parts, The = operator will not do the conversion, so if two values are not the same type = will simply return false. The = operator will compare for equality after doing any necessary type conversions. Reference: JavaScript Tutorial: Comparison Operators If(selected($)?)\.?)|localhost)(\:\d )?(/(?: /)*(?: (?:\?*)?(?:#*)?)?)?')įor more information about regular expressions, refer to the Mozilla Developer Network documentation.The strict equality operator ( =) behaves identically to the abstract equality operator ( =) except no type conversion is done, and the types must be the same to be considered equal. If the condition evaluates to true, returns a otherwise, returns b. Returns true if the given string ends with the substring.įits an existing date or time value to a defined format. This function also returns the number of attached files for image, audio, and file questions using the multiline appearance.Ĭonverts a number or string to a date object, without preserving time.ĭate-time(question, expression, or string)Ĭonverts a number or string to a date object.ĭecimal-date-time(question, expression, or string)Ĭonverts a date object to a decimal date-time number.ĭecimal-time(question, expression, or string)Ĭonverts a time object to a number representing a fractional day in the device's time zone. Returns the number of selected answers for select_one and select_multiple questions. A count value from outside the repeat can be referenced in a calculation inside the repeat. If the function is to be used in the Survey123 web app, it must be placed outside of the repeat. When used in the Survey123 field app, this function can be placed inside or outside of the repeat.
0 Comments
Leave a Reply. |