Separator for numeric literals:
We can use underscore as a separator for numeric literals . It is good to use for amount. It improves readability.
Before :
let amount = 100000;
After:
let amount = 1_00_000;
let price = 5_678.97
- You can use underscore with both integers, decimal values.
- You must use underscore only between digits. You get an error if you use it before or after digits.
- Below are valid declarations.
let amount1 = 10_000;
let amount2 = 5_876.98;
Below are invalid declarations
let amount1 = _1000;
let amount2 = 1000_;
let amount3 = 1000_.56;
You get below error .
Logical assignment operators:
Logical Nullish assignment(??=):
Nullish coalescing operator returns right side value if left side value is either null or undefined.
let fullName;
let fName = fullName??='Narayana Bojja';
console.log(fName);
Output:
Narayana Bojja
Here fullName variable doesn't have any data . It's value is undefined . So, It assigns right side value to fName variable.
let fullName='Bojja Narayana';
let fName = fullName??='Narayana Bojja';
console.log(fName);
Output:
Bojja Narayana
Here fullName variable has data . It assigns same data to fName variable. This operator useful if you want to make sure variable has some data.
Logical And Assignment (&&=):
It evaluates from left to right . It assigns value only if left side expression is true.
let x = 10;
let y = 20;
x &&= y;
console.log(x);
Output: 20
Here x has value . It will be evaluated as true. It assigns y value to x.
let x ;
let y = 20;
x &&= y;
console.log(x);
Output: undefined
Here x doesn't have value . It will be evaluated as false. It doesn't assign y value to x. This operator useful if you want to re assign variable with some data.
Logical OR Assignment (||=):
It works opposite to Logical And operator. If left side expression is false, it assigns right side value.
let x ;
let y = 20;
x ||= y;
console.log(x);
Output : 20
Here x doesn't have value . It will be evaluated as false. It assigns y value to x.
let x = 10;
let y = 20;
x ||= y;
console.log(x);
Output: 10
Here x has value . It will be evaluated as true. It doesn't assigns y value to x. This operator useful if you want to assign variable with some data in order to avoid any error.
String replaceAll method:
At present , there is replace method which replaces only first occurrence of a string with another string.
let fullName = "narayana narayana narayana"
fullName = fullName.replace('narayana', 'kavitha');
console.log(fullName);
Output:
kavitha narayana narayana
To replace all occurrences , we need to use regular expression.
let fullName = "narayana narayana narayana"
fullName = fullName.replace(/narayana/g, 'kavitha');
console.log(fullName);
Output:
kavitha kavitha kavitha
We can now use replaceAll method to replace all occurrences of a string with another string.
let fullName = "narayana narayana narayana"
fullName = fullName.replaceAll('narayana', 'kavitha');
console.log(fullName);
Output:
kavitha kavitha kavitha
I hope you have learned something new today. Please feel free to leave a comment below. Happy Coding 😃
If you would like to watch the same content, please watch below video of my YouTube channel Full Stack Adda
Good 👍 Explanation..
ReplyDeleteKeep it up