JavaScript Operators
Operators are important concepts in JavaScript and you may thing you already these basic things. But everyone must need to know some important things before appearing interviews. Because you will be asked only simple questions at the beginning. If you are strong enough in basic the recruiter asks tough questions.
Arithmetic Operators
- Additon ( + )
- Subtraction ( - )
- Multiplication ( * )
- Division ( / )
- Modules ( % )
Basic mathematics are same as normal mathematics but if you
want to get the remainder value of when you divide two number you need to use
modules operator. Note: Don't need to use brackets while using these operators,
but you can use it if you need. Here we used brackets to highlight the
operators only.
Concatenation
These operator not only for mathemetical caluculation, you can use it for join two different strings into a single strings. But we can concate two strings or array in different ways but we will see that later in some advanced topics.
Exponent
In mathemetics we can use square and cubes for mathematical calculations at some point, but here in JavaScript you need to use this exponent operator. eg: 2**3 = 8. You can change whatever the number instead of 3 in this example.
Increment
Usually if you want to increase the value of a number you need to use + operator but we can also use this + (addition) operator in different ways.
eg: usually if you want to increase a number with 1 means, we can do like this, x = x+1 , this will increase the value of x and store it to a x variable. But we can make this shorter like this: x++. This ++ operator will increse the current value by 1 and store it on the same varibale.
Decrement also same like the increment but instead of ++ you have to use --
Assignment Operators
By using this operator we can assign any data types into a varible. usally if you want to add a number in a existing value and save it on the same varible we do like this, let x = 1; x = x+1; or x++; But in assignment operator we can make the code shorter like this
x+=5, now this will give output as 6.
Simillarly for multiplcation, division and modules you can do use this method.
Comparison Operators
We can compare two values by using " == or === " if you use single equal sign (=) it will assign some values to a variable and the double equal used to compare the values between two variables but it will not check the type of the varibale. Example for double equal comparison: x = 3 == "3".
Here you can cleary see number 3 and string are not equal but the JavaScript consider both are equal and it will give true as output. But if you triple equal (===) symbol it will check the data type of both left and right and give output as false because number 3 or not equal to string 3.
These things are very important for API Calls, which we will see in details in advanced topics. API menas it will connect our frontend app to backend and get the data.
Mathematicals Operators: You can also you less than (<) or greater than (>) operators to compare between two numbers.
إرسال تعليق