1.Math :
ex: let price=200;
let result =0;
// Addition :
result = price + 100;
console.log( "Price + 100 = " + result.toString()); // Price + 100 = 300
// Subtraction
result = price-10;
console.log(" price - 10 = " + result.toString()); // price - 10 = 190
// Multiplication
result = price * 2;
console.log("price * 2 = " + result.toString()); // price * 2 = 400
// Division
result = price/2;
console.log("price / 2 = " + result.toString()); // price / 2 = 100
// Exponentiation
result = price ** 2;
console.log("price ** 2 = " + result.toString()); // price ** 2 = 40000
// Modulus
result = price % 3;
console.log(" price % 3 = "+ result.toString()); // price % 3 = 2
// Increment
result = price++;
console.log("result = price++ = "+ result.toString()); // result = price++ = 200
console.log("price = " + price.toString()); // price = 201
result = ++price;
console.log("result = ++price = " + result.toString()); // result = ++price = 202
// Decrement
result = price --;
console.log("result = price -- = " + result.toString()); // result = price -- = 202
console.log("price = " + price.toString()); // price = 201
result = --price;
console.log("result = -- price = " + result.toString()); // result = -- price = 200
Note :
When the ++ or -- is placed after the variable, the current value of the variable is retrieved prior
to the increment or decrement.
Using ++/-- after the operand :
when you use the increment/decrement operator after the operand, the value will be returned before the operand is increased/decreased.
ex :
let a =1;
console.log(a++); // 1
console.log(a); // 2
let b =1;
console.log(b--); // 1
console.log(b); // 0
The original value of the operand is being returned prior to the operand being changed.
Plus Sign with String and Number data types :
The plus sign is overloaded,so when you're using the string data type,
the plus sign is used to concatenate two strings together.
when you're using numeric data types, it is addition.
1.What if one is a number and one is a string?
ex : let result = 100 + '200';
if one is a string , it's going to do concatenation.
ex: let price = 200;
let stringValue = "100";
let result = 0;
result = price + stringValue;
console.log("result ( number + stringValue) = " + result.toString()); // 200100
result = price + (+stringValue);
console.log(" result ( number + (+ stringValue)) = " + result.toString()); // 300
Note : Javascript looks at both operands and determines their data type prior to applying the plus sign.
The plus sign immediately prior to a string variable converts that string to a numeric data type if possible.
Comparison and Ternary Operators :
1.The == operator only checks to see if the value is the same, not the data type.
2. The === operator checks both the value and the data type to see if they are equal.
The ternary is like an if...else statement expressed on one line.
syntax : result = exp ? (exp is true) : (exp is false)
ex : let insurancePremium = age > 21 ? 100 : 200;
Note : shorten your if statement into one line of code with the conditional operator.
we can nest ternary operators to test multiple conditions.
ex : let price = isStudent ? 8 : isSenior ? 6 : 10
it is also possible to run multiple operations within a ternary.To do this,
we must separate the operations with a comma.
ex: let a=1;
let b=2;
a===b ? (a=1,b=2) : (a=2,b=1);
console.log(a); // 2
console.log(b); // 1
The Effect of 'use strict' :
'use strict' is a statement that you should always be using.
1.'use strict' is ignored by older browsers.
2.With 'use strict' you must declare all variables before using them.
3.you can't use reserved words as variables, so eval or arguments.you can't delete
a variable, and you can't delete a function when you have use strict in effect.
ex:
// Can't use reserved words as variables
let eval =10;
let arguments ="some args";
// can't delete a variable
delete result;
// can't delete a function
delete useStrictSample;
No comments:
Post a Comment