Hello
Buddies,
Here
are 2 javascript encounters, few more are about to come.
1. While
working with JSP and JS, a person habitual of using debug mode in
Eclipse, might feel that something is missing, in understanding the UI
flow.
For
such incidents, one can always use either of the ways to confirm what
values are being set or how the flow is being considered.
Common
UI
debugging techniques -
1.
console.log()
- to
print variables’ values in javascripts, without impacting the UI.
2.
F12
debugs
– it gives you power to put breakpoints in the scripts listed in
‘sources’ window, check the requests and responses of various
APIs invoked, time taken for the calls made, as well as make minor
changes in the style section, or other HTML components.
3.
document.write()
– it
writes any value you want to display on the UI, without any format.
4.
alert()
-
it shows the immediate values of javascripts’ variables, and this
method is often used by developers to make sure if the desired
javascript is invoked.
2. Secondly,
while
having the
hands on
with some javascript basics, encountered a behavior which
I felt might come handy in evaluating javascript skills.
Quoted
literals
(either
single or double quotes), in a mathematical expression, will make the
entire line as a string, somewhat like concatenation, as far as I
could understand.
Following
are some observations -
2
+ 3 * 5 = 17
'2'
+ 3 * 5 = 215
'2'
+ '3' * 5 = 215
'2'
+ '3' * '5' = 215
'2'
+ '3' - '5' = 18
'2'
+ '3' / '5' = 20.6
So
beware of such scenarios if asked. What we observed here is that
though the literals are being regarded as string, but that holds true
only or ‘+’ operator. For any other mathematical operator,
literals are still regarded as numbers, and the results will be
accordingly. Please
have a thorough look at the above results.
Feel
free to put your inputs.
Happy
coding.
No comments:
Post a Comment