Notes On Operators-3262
Notes On Operators-3262
Post-increment/decrement operator
The post-increment/decrement operator (++) and (--) first use the current value of a
variable and then increment/decrement it. For example:
In the above example, x++ returns the value of x (which is 1) and then increments x
by 1 (so x becomes 2).
Pre-increment/decrement operator
The pre-increment/decrement operator (++x) and (--x) first increment/decrement the
value of a variable and then use it. For example:
In the above example, ++y increments y by 1 (so y becomes 2) and then returns the
new value of y (which is also 2).
Both types of operators can be used with any variable that holds a numeric value,
including variables that are defined using the var, let, and const keywords.
The example given below demonstrates the behavior of pre- and post-decrement
operators and how they can affect the value of a variable in JavaScript.
Explanation:
Line 1: The initial value of the variable num is set to 5.
Line 2: The console.log(--num); statement uses the pre-decrement operator to
decrement num by 1 before printing its value to the console. The output is 4.
Line 3: The console.log(num); statement prints the value of num, which is 4 because
it was previously decremented.
Line 4: The console.log(num++); statement uses the post-decrement operator to
print the value of num (which is still 4), and then decrement num by 1. The output is
4.
Line 5: The console.log(num); statement prints the value of num, which is now 5
because it was incremented after the third console.log statement.
In JavaScript (JS), strings are compared using Unicode values, which is a standard
for assigning unique numeric values to different characters from various writing
systems around the world.
Unicode Values
● Unicode represents characters as numeric values, which is important for
string manipulation and comparison operations.
String Comparison
When comparing two strings, JS compares each character's Unicode value at the
same index in each string. If the Unicode values are the same, it moves to the next
character's Unicode value until it finds a difference or reaches the end of both
strings.
In the above example, the str1 is compared to str2 using Unicode values. The
Unicode value of the first character in str1 (which is 'a') is less than the Unicode
value of the first character in str2 (which is 'b'). Therefore, str1 is less than str2, and
the output is true.
● It's important to note that when comparing strings with different character
lengths, JS compares each character up to the length of the shorter string. If
all the characters are the same up to the length of the shorter string, then the
longer string is considered greater.