Every number in JavaScript is truthy except “0”. If a variable contains “0”, then it’s a falsy value. You can check them…
var num = 0;if (num){
console.log("Condition is true")
} else {
console.log("Condition is false")
}// Output: Condition is falsenum = 1;if (num){
console.log("Condition is true")
} else {
console.log("Condition is false")
}// Output: Condition is truenum = -1;if (num){
console.log("Condition is true")
} else {
console.log("Condition is false")
}// Output: Condition is true
So here we can see that all natural numbers are truthy except zero(0).
In JavaScript, every string is truthy except an empty string (“”). If a variable contains an empty string, then it’s a falsy value. …
JSX looks like a regular HTML in most cases. JSX is an HTML-like syntax used by React that extends ECMAScript so that HTML-like text can co-exist with JavaScript/React code. The syntax is intended to be used by preprocessors (i.e., transpilers like Babel) to transform HTML-like text found in JavaScript files into standard JavaScript objects that a JavaScript engine will parse.
By using JSX you can write HTML-like structures in the same file as you write JavaScript code, then Babel will transform these expressions into actual JavaScript code. …
Primitive Values are numbers and strings, among other things. It can contain only a single thing (be it a string or a number or whatever).
let n = 231;
n = 221;
The number type in JavaScript represents both integer and floating-point numbers. There are many operations for number type, e.g. multiplication (*), division (/), addition (+), subtraction (-), and so on.
// "n" at the end of the value means it's a BigInt typeconst bigInt = 123456234589012345677868923456789234234567890n;
A BigInt value is created by appending n to the end of an integer. In JavaScript, the “number” type cannot represent integer values larger than (2^53–1) (that’s 9007199254740991), or less than -(2^53–1) for negatives. …
What you usually do when you have to iterate through every item in an array? Most probably you run for loop in that array. But there is a method built-in with JavaScript which is called map(). Here is an example of it.
let numbers = [18, 21, 32, 49, 5, 6, 7, 8];
const mapResult = numbers.map(number => number * 2);
console.log(mapResult)// Expected output
// [36, 42, 64, 98,10, 12, 14, 16]
The map method takes an argument which is every element of an array. Then it does some operations with that element and returns a new array.
The filter method is used when you have to filter some elements from an array if they pass the test implemented by the provided function. It is helpful when you have a large array and want to filter some items of the same category. Let’s see how it…
In JavaScript, you can declare a variable using three keywords. These are var, const, and let. Now you might be thinking, “What is the difference between these keywords? These do the same thing which is declaring a variable.” No, they are not the same. Let’s see this example below…
// bad
var a = 1;
var b = 2;// good
const a = 1;
const b = 2;// good (But not in all cases)
let a = 1;
let b = 2;
In modern JavaScript, we don’t use var. Rather we use let and const. The only difference between let and const is, you can change the value of a variable which has declared using let but you can’t change the value of a variable which has declared using const. …
You should use let or const when you define a variable. You might be asking, “Why should I do that?” Let’s see some examples of differences between let, const, and var then you will get your answer.
const ages = [4, 5, 6, 7, 8, 9]
for (var i = 0; i < ages.length; i++) {
// Do your stuff...
}
console.log(i)Output: 6
Here you can see that by declaring a variable using the var keyword, you can call it outside of the block where you have declared that variable.
But what if you use let keyword instead of var? …
About