A comparison operator returns a Boolean value indicating that the comparison is true or not. See the following example:
let r1 = 20 > 10; // true let r2 = 20 < 10; // false let r3 = 10 == 10; // true
let a = 10, b = 20; console.log(a >= b); // false console.log(a == 10); // true
This example is straightforward. the variable
a>=b expression returns and
a==10 expression returns
let name1 = 'alice', name2 = 'bob'; let result = name1 < name2; console.log(result); // true console.log(name1 == 'alice'); // true
let f1 = 'apple', f2 = 'Banana'; let result = f2 < f1; console.log(result); // true
In this example,
f2 is less than
f1 because the letter
B has the character code
66 while the letter
a has the character code
To fix this, you must first convert strings into a common format, either lowercase or uppercase and then perform comparison as follows:
let result2 = f2.toLowerCase() < f1.toLowerCase(); console.log(result2); // false
Note that the
toLowerCase() is a method of the String object that converts the string itself to lowercase.
The compound assignment operators consist of a binary operator and the simple assignment operator.
The binary operators, work with two operands. For example a+b where + is the operator and the a, b are operands. Simple assignment operator is used to assign values to a variable(s).
It’s quite common to modify values stored in variables. To make this process a little quicker, we use compound assignment operators.
You can also check my video tutorial compound assignment operators.
Let’s consider an example. Suppose price = 5 and we want to add ten more to it.
var price = 5;
price = price + 10;
We added ten to price. Look at the repetitive price variable. We could easily use a compound += to reduce this. We do this instead.
price += 5;
Awesome. Isn’t it? What’s the value of price now? Practice and comment below. If you don’t know how to practice check these lessons.
Lets bring down the price by 5 again and display it.
We use console.log command to display what is stored in the variable. It is very help for debugging.
Debugging let’s you find errors or bugs in your code. More on this later.
price -= 5;
Lets multiply price and show it.
and finally we will divide it.
If you have any doubts, comment below.
Does your business need an interactive website or app?
The technology used to develop the overall app by the developers from WebClues Infotech is at par with the latest available technology.
For more inquiry click here https://bit.ly/31eZyDZ
Book Free Interview: https://bit.ly/3dDShFg
What is a ternary operator: The ternary operator is a conditional expression that means this is a comparison operator and results come on a true or false condition and it is the shortest way to writing an if-else statement. It is a condition in a single line replacing the multiline if-else code.
syntax : condition ? value_if_true : value_if_false
condition: A boolean expression evaluates true or false
value_if_true: a value to be assigned if the expression is evaluated to true.
value_if_false: A value to be assigned if the expression is evaluated to false.
How to use ternary operator in python here are some examples of Python ternary operator if-else.
Brief description of examples we have to take two variables a and b. The value of a is 10 and b is 20. find the minimum number using a ternary operator with one line of code. ( **min = a if a < b else b ) **. if a less than b then print a otherwise print b and second examples are the same as first and the third example is check number is even or odd.
#python #python ternary operator #ternary operator #ternary operator in if-else #ternary operator in python #ternary operator with dict #ternary operator with lambda