JavaScript Mini Series

Apurbo Kumar Paul
2 min readMay 8, 2021

Javascript is the most important and powerful programming language for web development.We can use others programming language but to me javascript is the easiest of them to make any kind of web based project.

I will discuss some basic of javascript -

Truthy vs Falsy values

In JavaScript, if/else statement checks if a value is truthy or falsy. A truthy value is like true and a falsy value is like false.Here are some example -

falsy  -> false, '', "", 0, -0, 0n, NaN, null, undefinedconsole.log(NaN ? "Yes" : "No") // Output - Noconsole.log(0 ? "Yes" : "No") // Output - Notruthy -> anything that is not mentioned aboveconsole.log(1 ? "Yes" : "No") // Output - Yesconsole.log("Hello" ? "Yes" : "No") // Output - Yes

Double equal (==)

== in JavaScript is used for comparing two variables, but it ignores the datatype of variable.The == checks for equality only after doing necessary conversations.

48 == "48" //true

Triple equal (===)

=== is used for comparing two variables, but this operator also checks datatype and compares two values.If two variable values are not similar, then === will not perform any conversion.

48 === "48" // false

Understanding scope

Block scope is within curly brackets.

Example: var age = 10;
if (age > 9){
var dogYears = age * 7;
console.log(`You are ${dogYears} dog years old!`);
}
//Output-
10
You are 70 dog years old!

Global variable, Local variable

//Global variable
var age = 22;// global variable
function a(){
}
//Local variable
function a(){
var age = 22;// local variable
}

Find the largest element of an array

var arr = [1, 2, 3];
var max = Math.max(...arr);

Remove duplicate item from an array

let chars = [‘A’, ‘B’, ‘A’, ‘C’, ‘B’];
let uniqueChars = […new Set(chars)];
console.log(uniqueChars);

Count the number of words in a string

<html>
<body>
<script>
function countWords(str) {
str = str.replace(/(^\s*)|(\s*$)/gi,"");
str = str.replace(/[ ]{2,}/gi," ");
str = str.replace(/\n /,"\n");
return str.split(' ').length;
}
document.write(countWords(" Tutorix is one of the best E-learning platforms"));
</script>
</body>
</html>

Reverse a string

// program to reverse a stringfunction reverseString(str) {// empty string
let newString = "";
for (let i = str.length - 1; i >= 0; i--) {
newString += str[i];
}
return newString;
}
// take input from the user
const string = prompt('Enter a string: ');
const result = reverseString(string);
console.log(result);

Calculate Factorial in a Recursive function

let countDown = function f(fromNumber) {
console.log(fromNumber);
let nextNumber = fromNumber - 1;if (nextNumber > 0) {
f(nextNumber);
}
}
let newYearCountDown = countDown;
countDown = null;
newYearCountDown(10);

That’s all for today…

--

--