Home > Article > Web Front-end > A brief discussion on variable initialization in js_javascript skills
I wrote a function in js that removes leading and trailing spaces and specific characters. The code is as follows:
The code looks fine and runs without errors.
Until today, when I searched for "note3" in the search bar, I found that the search content in the address bar changed to "ote3", while other letters or numbers starting with it are normal. What to search for, address That's what's in the column.
After debugging the code, change the code to:
Added initialization of charlist in the function. When searching for "note3", the result is correct.
Although js is a weakly typed language, using variables does not require initialization. When running, the code will automatically convert and assign values. However, doing so will cause some unexpected problems, so it is very necessary to initialize all variables used.
That’s it for today, I hope you guys like it.