Everything About JavaScript Basics

For many years JavaScript is a very popular programming language, especially in the web development industry. So, if someone trying to make a bright career in this industry you should learn JavaScript. Not only web development if want to be any kind of programmer and you can’t decide which programming language you should choose, simply start learning JavaScript. Because JavaScript is easy to learn and you can do everything with JavaScript in 2021. If you learn any programming language it is easy to switch to another programming language.

In this blog, I am not going to teach you from scratch. Because is not Documentation of JavaScript. If you don’t know anything about JavaScript, I will recommend you to visit this website. We are going to learn about JavaScript Data Type’s methods with real examples. So, let’s get into it.

Length

In my point of view, Length is the most used method in any programming language. Using the Length method we can find out how many characters exist in a string. Not only string, but we can also use this method for array and other data types. Suppose you want an input text including 6 characters from users. It is not necessary that all the users will give a text including 6 characters, so we need to validate whether it is 6 characters or not using the Length method.

var userInput = “abcdef”userInput.length // output 6var userInput = “abced”userInput.length // output 5

parseInt() / parseFloat()

parseInt and parseFloat are another most important method in programming. By this method, we can convert a string data type to a number data type. For example, you need an input number from users and you will add 10 with it. Suppose, one user gives an input number 20. So, result will be 20 + 10 = 30. What if the user gives a string number like this “20”. If you add 10 with “20” the output will be NaN. In this situation, you need to convert “20” to 20 using parseInt. You can also convert a decimal string to a number like “20.22” to 20.22.

var userInput = 5var result = userInput + 10 //output 15var userInput = “5”var result = userInput + 10 //output NaN

typeof

typeof is a javascript method by this method we can check the type of any data. There are few data types in JavaScript such as string, number, object, boolean, undefined. Suppose, you need a number input from a user but the user can give any type of data like string, number, object, boolean. So, you need to check the type of user’s input if it is then it will be good but if it is not then you have to give an error message to the user.

var userInput = 5typeof userInput //output numbervar userInput = ‘abcd’typeof userInput //output string

includes

includes is another JavaScript method to check whether a string has any word or not. If the word will be in the string output will be true, if the word will not be there then the output will be false. Suppose, you want a short description of your users. But users can write anything about even some bad words. So, you need to check if there are any bad words. If there then you will give an error to the user.

var description = “Hello, my name is Ahnaf Abir. I am a web developer”description.includes(“Banker”) // output falsevar description = “Hello, my name is Ahnaf Abir. I am a web developer”description.includes(“developer”) // output true

toLowercase / toUppercase

These methods are one of the most important methods for the string data type. By these two methods, we can convert a string to lowercase or uppercase. Suppose, you want to get a username from a user and we all know that username always is in lowercase. But the user can also give a username that is in uppercase like this “AHNAFABIR11”. So, we need to convert this username to this “ahnafabir11” using the toLowerCase method.

var username = “AHNAFABIR11”username.toLowerCase() // output ahnafabir11

trim, trimStart, trimEnd

trim is another most important method in JavaScript. Trim is used to removing blank space from a string. Blank space could be at starting and also at the end. If you want to remove black space from both starting and ending you need to use the trim() method, if you want to remove blank space separately you need to use the trimStart() and trimEnd() method.

var userInput = “ Hello, World ”console.log(userInput) // output “ Hello, World ”var userInput = “ Hello, World ”userInput.trim() // output “Hello, World”

NaN

NaN is one of the most confusing things in JavaScript. The full form of NaN is Not A Number. Let’s understand what exactly NaN is. Suppose, you have a string in a variable call x and you are trying to add 10 (a number) with this string. It is impossible to add a number with a string. So, the output will be NaN. We can also check whether the output is NaN or not using the isNaN method.

var x = “hello”x + 10 // output NaNNumber.isNaN(NaN) // output trueNumber.isNaN(“hello”) // output falseNumber.isNaN(1) // output false

ceil / floor / round

ceil, floor, and round are the most important methods for Number data types. There are two types of number integer and float. 102 is an integer number and 102.324 is a float number. Sometimes you need to make a float number to an integer. Suppose, you have a float number like this 32.45, you need to make whether 32 or 33. It will be easy to understand seeing the example below.

var number = 32.45Math.ceil(number) // output 33Math.floor(number) // output 32var number = 15.49 // < 15.50Math.round(number) // output 15var number = 15.56 // > 15.50Math.round(number) // output 16

push / pop

push and pop are two very common methods for JavaScript array. push adds a new array item at the end and pop remove an array item from the end. There is one similar thing between push and pop, this is they both work at the end position. You will understand better by the example below -

var animals = [‘dog’, ‘cat’]animals.push(‘cow’) // output [‘dog’, ‘cat’, ‘cow’]var animals = [‘dog’, ‘cat’]animals.pop() // output [‘dog’]

shift / unshift

Previously, we talk about push and pop. shift and unshift simply the opposite of push and pop. pop and push work with the end position of an array, on the other hand, shift and unshift work with the first position of an array. The example below will help you to understand better -

var animals = [‘dog’, ‘cat’, ‘cow’]animals.shift() // output [‘cat’, ‘cow’]var animals = [‘dog’, ‘cat’, ‘cow’]animals.unshift(‘bird’) // output [‘bird’,‘dog’, ‘cat’, ‘cow’]