Variables and Operators

Declaring (Creating) JavaScript Variables

JavaScript variables can hold numbers like 100 and text values like "John Doe". In programming, text values are called text strings. JavaScript can handle many types of data, but for now, just think of numbers and strings. Strings are written inside double or single quotes. Numbers are written without quotes. If you put a number in quotes, it will be treated as a text string.

var pi = 3.14;
var person = "John Doe";
var answer = 'Yes I am!';

JavaScript Arithmetic Operators

Let's understand this with example :- 

var A = 10;
var B = 20;

Example of Operators and Their output

No. Operator and Description
1

+ (Addition)

Adds two operands

Ex: A + B will give 30

2

- (Subtraction)

Subtracts the second operand from the first

Ex: A - B will give -10

3

* (Multiplication)

Multiply both operands

Ex: A * B will give 200

4

/ (Division)

Divide the numerator by the denominator

Ex: B / A will give 2

5

% (Modulus)

Outputs the remainder of an integer division

Ex: B % A will give 0

6

++ (Increment)

Increases an integer value by one

Ex: A++ will give 11

7

-- (Decrement)

Decreases an integer value by one

Ex: A-- will give 9

JavaScript Assignment Operators

Assignment operators assign values to JavaScript variables.

No Operator and Description
1

= (Simple Assignment )

Assigns values from the right side operand to the left side operand

Ex: C = A + B will assign the value of A + B into C

2

+= (Add and Assignment)

It adds the right operand to the left operand and assigns the result to the left operand.

Ex: C += A is equivalent to C = C + A

3

?= (Subtract and Assignment)

It subtracts the right operand from the left operand and assigns the result to the left operand.

Ex: C -= A is equivalent to C = C - A

4

*= (Multiply and Assignment)

It multiplies the right operand with the left operand and assigns the result to the left operand.

Ex: C *= A is equivalent to C = C * A

5

/= (Divide and Assignment)

It divides the left operand with the right operand and assigns the result to the left operand.

Ex: C /= A is equivalent to C = C / A

6

%= (Modules and Assignment)

It takes modulus using two operands and assigns the result to the left operand.

Ex: C %= A is equivalent to C = C % A

JavaScript Comparison Operators

Let's understand with example and their output :-

var A = 10;
var B = 20;
No Operator and Description
1

= = (Equal)

Checks if the value of two operands are equal or not, if yes, then the condition becomes true.

Ex:  (A == B)  is not true.

2

!= (Not Equal)

Checks if the value of two operands are equal or not, if the values are not equal, then the condition becomes true.

Ex: (A != B) is true.

3

> (Greater than)

Checks if the value of the left operand is greater than the value of the right operand, if yes, then the condition becomes true.

Ex: (A > B) is not true.

4

< (Less than)

Checks if the value of the left operand is less than the value of the right operand, if yes, then the condition becomes true.

Ex: (A < B) is true.

5

>= (Greater than or Equal to)

Checks if the value of the left operand is greater than or equal to the value of the right operand, if yes, then the condition becomes true.

Ex: (A >= B) is not true.

6

<= (Less than or Equal to)

Checks if the value of the left operand is less than or equal to the value of the right operand, if yes, then the condition becomes true.

Ex: (A <= B) is true.

JavaScript Logical Operators

Let's understand with Example and Their output :

var A = 10;
var B = 20;
No Operator and Description
1

&& (Logical AND)

If both the operands are non-zero, then the condition becomes true.

Ex: (A && B) is true.

2

|| (Logical OR)

If any of the two operands are non-zero, then the condition becomes true.

Ex: (A || B) is true.

3

! (Logical NOT)

Reverses the logical state of its operand. If a condition is true, then the Logical NOT operator will make it false.

Ex: ! (A && B) is false.

Miscellaneous Operator

Ternary Operator

No. Operator and Description
1

? : (Conditional )

If Condition is true? Then value X : Otherwise value Y

JavaScript Type Operators

Operator Description
typeof Returns the type of a variable
instanceof Returns true if an object is an instance of an object type

Loading ...

Related Results :

  1. [How-to] Setup Custom full root or Sub-Domain for Profile and Course
  2. Introduction to HTML
  3. HTML Basic Tags and Elements
  4. HTML Attributes
  5. HTML Formatting
  6. HTML Phrase Formatting
  7. HTML Comment Tags
  8. HTML Meta and OG (Opengraph) Tags
  9. HTML Images
  10. HTML Tables
  11. HTML Lists
  12. Introduction to Python
  13. Variables and Types
  14. Lists, Tuples & Dictionary
  15. Basic Operators
  16. If else-elif else nested if Statement
  17. For & While Loops
  18. String Formatting
  19. Functions in Python
  20. Basic String Operations
  21. Classes and Objects
  22. self in python class
  23. Built-In & More Classes & Objects
  24. Modules and Packages
  25. Introduction to CSS
  26. CSS Syntax & Selectors
  27. Include CSS
  28. CSS Measuring Units
  29. CSS Colors
  30. CSS - Background
Note :
  • Related Posts are generally User Blog posts.
  • or Other tutorials from other networks of w3clan.com.
  • Any registered user can create related posts based on search term tags.

About the Author