Why showing error in my code ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
30th May 2021, 4:21 AM
๐—ฃ๐—ฎ๐˜„๐—ฎ๐—ป ๐—ž๐˜‚๐—บ๐—ฎ๐—ฟ ๐Ÿ…Ÿ๏ธŽ๐Ÿ…š๏ธŽ
๐—ฃ๐—ฎ๐˜„๐—ฎ๐—ป ๐—ž๐˜‚๐—บ๐—ฎ๐—ฟ ๐Ÿ…Ÿ๏ธŽ๐Ÿ…š๏ธŽ - avatar
4 Answers
+ 3
because you use global variables, and global variables are properties of 'window'... as 'window' object has a still a property named 'name', wich is used to hold a string, any value assigned to it would be casted to a string. String objects have a match method, while Number object doesn't have such method (regex are used only on String). that's the reason why storing a number to the variable 'name' (despite the 'var' declaration) cast it to a string, and allow to call 'match' method on it ;)
30th May 2021, 4:44 AM
visph
visph - avatar
0
It's mean name is a global variable and it has string property. Do you have another way to assign value in number variable.
30th May 2021, 8:20 AM
๐—ฃ๐—ฎ๐˜„๐—ฎ๐—ป ๐—ž๐˜‚๐—บ๐—ฎ๐—ฟ ๐Ÿ…Ÿ๏ธŽ๐Ÿ…š๏ธŽ
๐—ฃ๐—ฎ๐˜„๐—ฎ๐—ป ๐—ž๐˜‚๐—บ๐—ฎ๐—ฟ ๐Ÿ…Ÿ๏ธŽ๐Ÿ…š๏ธŽ - avatar
0
Thanks buddy
30th May 2021, 8:20 AM
๐—ฃ๐—ฎ๐˜„๐—ฎ๐—ป ๐—ž๐˜‚๐—บ๐—ฎ๐—ฟ ๐Ÿ…Ÿ๏ธŽ๐Ÿ…š๏ธŽ
๐—ฃ๐—ฎ๐˜„๐—ฎ๐—ป ๐—ž๐˜‚๐—บ๐—ฎ๐—ฟ ๐Ÿ…Ÿ๏ธŽ๐Ÿ…š๏ธŽ - avatar
0
'name' is a builtin property of 'window', so its scope is the global scope... you can access it either by 'name', 'window.name', or even 'window["name"]... it's a kind of getter/setter wich convert any thing assigned to it as string: https://developer.mozilla.org/en-US/docs/Web/API/Window/name if you declare a variable in a local scope, then 'name' will refer to your 'normal' variable (but you still can access the property of 'window' through dot/bracket notation ;) in js all variable are usually duck typing, do I don't understand what you mean by "another way to assign value in NUMBER variable"?
30th May 2021, 8:29 AM
visph
visph - avatar