Logical Operator
Logical operators are used to perform Boolean operations on Boolean operands. Logical operators, too, return true or false results based on the evaluation of the expression. Logical operators are:
Operator |
Description |
Example |
&& |
Logical and. If both Boolean operands on left and right side are true, only then the result is true, else false. |
x = True ; y = False;
Result : False |
| | |
Logical or. If either or both expressions evaluate to true, result is true. |
x = True ; y = False;
Result : True |
! |
Logical not. It negates the value of the Boolean expression. |
x = True ;
Result : False |
| |
|
|
Conditional Operator
Conditional operator is a ternary operator as it takes three operands. It is used to execute one of the two statements depending on condition. The conditional operator contains a condition to be evaluated, and two alternative values to be returned based on the truth or falsity of the condition.
| Syntax |
| Condition ? Value 1 : Value 2 |
If the condition is true, value 1 is the result of the expression, otherwise value 2 is the result of the expression.
| Example |
x = 5 ? odd : even.
The result will be True. |
String Operator
String operators are those operators that are used to perform operations on strings. Currently JavaScript supports only the 'string concatenation (+)' operator.
| Example |
| "Java" + "Script" = "JavaScript" |
Sample question
If a= 10 and b=4, then print the value of 'a' after each of the following operations :
a+=b View Code View Run time
a-=b View Code View Run time
a*=b View Code View Run time
a/=b View Code View Run time
| |
| |
| Summary |
In this chapter you have learnt:
- How data is stored using variables.
- Why naming of variables is important.
- Local and Global scope of variables.
- Various operators used in JavaScript.
|
| |
| Review Questions |
| |
Fill in the Blanks
- -------- are used to store values.
- Variables can be declared using ------ command.
- -------- operator takes one operand only.
- Conditional operator takes ------ operands.
Solutions
- Variables
- var
- Unary
- three
Check if following variable names are possible or not
- var Abc
- var XYZ
- var $aa
- var 1iu
- var __
- var $_1
- var aa aa
Solutions
- Valid variable name.
- Valid variable name.
- Valid variable name.
- Invalid variable name, because variable name cannot start with a number.
- Valid variable name.
- Valid variable name.
- Invalid variable name, because variable name cannot include space.
|
| |
| What's Next |
s
The next chapter will acquaint you with the concepts of JavaScript programming through looping and functions. The chapter will further elucidate various loops available in JavaScript and how to create functions in JavaScript.
Hop over to the next chapter to get the close-up of JavaScript loops and functions.
|
|
|