10 JavaScript must-know concepts for the beginners
JavaScript is a programming language. It is very popular across the world. For beginners, there are many confusing things in JavaScript. I am trying to clear some confusion about JS string and JS math. I hope I can clear your confusion….Let’s start
Firstly we are talking about JS String
.charAt
If you want to know the position of any character in a string you have to use the (.charAt) method.
Example-
We have a string named greeting.
const greeting = ‘Hello, Good morning.’;
We want to find out the letter in index 4.
const index = 4;
console.log(`The character at index ${index} is ${greeting.charAt(index)}`);
Ans: “The character at index 4 is o”
.concat
If you want to join two string along you can use the (.concat) method to do that
Example-
We have two different strings
const string1 = “Good”;
const string2 = “morning”;
We want to join them together by concat
console.log(string1.concat(‘ ‘, string2));
Ans: Good morning
.trim
If you want to remove whitespaces from both sides of a string you can use the (.trim) method
Example-
We have a string named greeting with whitespaces on both side
const greeting = “ Hello, good morning! ”;
We want to remove them by applying the trim method
console.log(greeting.trim());
Ans: “Hello, good morning!”
.replace
If you want to replace any word from a string just use the (.replace) method
Example-
We have a string
const quote = ‘Our world is so beautiful. Isn’t it?’;
We want to change the word ‘beautiful’ to ‘nice’
console.log(quote.replace(“beautiful”, “nice”));
Ans: Our world is so nice. Isn’t it?
.toLowerCase and .toUpperCase
If you want to make a string to lowercase just use (.toLowerCase) and want to make uppercase use the (.toUpperCase) method
Example-
const quote = “We all love our motherland” ;
We want to make a lower case
console.log(quote.toLowerCase());
Ans: we all love our motherland
Make the uppercase
console.log(quote.toUpperCase());
Ans: WE ALL LOVE OUR MOTHERLAND
Now, we are talking about JS Number
.round
If you want to convert any decimal number to a round figure. You can use (Math.round) method
Example-
console.log(Math.round(5.95));
Ans: 6
console.log(Math.round(5.05));
Ans: 5
console.log(Math.round(-5.05));
Ans: -5
.min and .max
If you want to find out the minimum or maximum number from an array you can use (Math.min) to find the minimum number and (Math.max) to find the maximum number
Example-
We have an array
const array1 = [2, 3, 1, 4, 7, 12];
We want to find the minimum number from the array
console.log(Math.min(…array1));
Ans: 1
Now, we want to find the maximum number from the same array
console.log(Math.max(…array1));
Ans: 12
Note: we can use the array right away in the console line like this
console.log(Math.max(2, 3, 1, 4, 7, 12));
The answer will be remain same.
. random
If you want a random number from the same number for a lucky draw or something else you can use (Math.random)
Example-
We can get random numbers from 0 to 10 by using this code
console.log(Math.random(2)*10);
Ans: Any number between 0–10.
Note: if you want to make the number integer you can use round or other methods to do that.
.ceil
If you want to convert any decimal number into the upper integer number just use (Math.ceil) method
Example-
console.log(Math.ceil(.95));
Ans: 1
console.log(Math.ceil(4));
Ans: 4
console.log(Math.ceil(7.004));
Ans: 8
console.log(Math.ceil(-7.004));
Ans: -7
.floor
If you want to convert any decimal number into the lower integer number just use the (Math.floor) method
Example-
console.log(Math.floor(5.95));
Ans: 5
console.log(Math.floor(5.05));
Ans: 5
console.log(Math.floor(5));
Ans: 5
console.log(Math.floor(-5.05));
Ans: 6
Hope you enjoy the learning. That’s not the end of javaScript. I will try to clear all the confusing things step by step. Follow me and stay connected. See you…