Javascript fundamentals. (Part-01)

Today I want to share some Javascript fundamental topics which we use in day to day life working in Javascript language.

1.String Concat() method:

String concat() method is used to add two or more strings. This method doesn’t change the previous string but returns a new string.

Example:

console.log(“Fardin” + “ Ahmed”)

// Output : Fardin Ahmed

(Example: 01)

We can also write this way :

var name = “Fardin”

console.log(name + “ Ahmed”);

// Output : Fardin Ahmed

(Example : 02)

In both cases The process is same. In 1st Example i showed directly and in 2nd Example i store a sting in a variable called “name”. Then i concated another string with “name” variable. So, simple concept to understand.

2.String Indexof() :

String Indexof() method is used to find out the position of a string. Indexing starts from 0(zero). The Indexof() method returns -1 for invalid search.

Example:

var name = “Lets learn Javascript”

var position = name.indexOf(“L”)

console.log(position)

// Output : 0

We wrote a variable which is “name”. In the “name” variable we stored a string. Then we wrote a variable named “position” and used indexof() method. We wrote “L” inside of indexof() parenthesis. Then we declared the “position” variable in console.log(). Compiler starts checking. As the first word is “L” so we got the output. We already know that indexing starts from zero(0) so “L” is situated in the 1st position thats why output is 0(zero).

var name = “Lets learn Javascript”

var position = name.indexOf(“J”)

console.log(position)

// Output : 11

If we start count from zero(0) which is “L” then we find “J” in 11th number position.

[N.B : Compiler also count white space]

3.String Slice() method:

The slice method cuts a part of string. It has a start parameter and end parameter. Concept will clear after watching the example.

Example:

var start = “Javascript is awesome”

var result = start.slice(4)

console.log(result)

// Output : script is fun

(Example: 01)

Counting starts from zero(0) which is “J”. In the slice() method we wrote only the start position and thats why compiler starts counting from position 4 and prints rest of the words.

Now let’s see with end parameter :

var start = “Javascript is awesome”

var result = start.slice(0, 10)

console.log(result)

// Output : Javascript

(Example: 02)

So, the start parameter is 0. Compiler starts counting from 0 and compilers will continue the counting untill it reaches 10th position. 10th number position is a white space (“ ”).

var start = “Javascript is awesome”

var result = start.slice(0, 9)

console.log(result)

// Output : Javascrip

(Example: 03)

Example : 03 might confuse you. But wait let me explain first. Our common sense says 9th position is “t” so it should print “Javascript”. But, In The end parameter if we write (9) compiler will ignore the 9th position and prints from 0 to 8 which is “Javascrip” so compile will ignore the last position of end parameter.

4.String toUpperCase() method:

If we need to transform the small letter into capital letter then we use toUpperCase() method. This method is very simple to use.

Example:

var start = “javascript is awesome”

var result = start.toUpperCase()

console.log(result)

// Output : JAVASCRIPT IS AWESOME

(Example: 01)

In “start” variable every letter inside of string in lowercase. We use toUpperCase() method to transform them in Capital letter and then output is Capital.

5.String toLowerCase() method:

If we need to transform the capital letter into small letter then we use toLowerCase() method. This method is very simple to use.

Example:

var upper = “JAVASCRIPT IS AWESOME”

var lower = upper.toLowerCase()

console.log(lower)

// Output : javascript is awesome

In “upper” variable every letter inside of string in Upper Case. We use toLowerCase() method to transform them in small letter and then output is in smallCase.

6. Javascript Basic Numbers :

  • Number is a primitive data type.

7. Javascript parseInt() method in Numbers :

We can convert a (fractional/floating) number into an integer number using parseInt() method.

Example:

var nn = Number(45.45)

console.log(Number.parseInt(nn))

// Output : 45

(Example: 01)

In Example 01 we wrote a fractional number inside of a Number() data type and store that in “nn” variable. Then we used parseInt() method to convert the floating number(45.45) into an integer number(45).

8. Javascript parseFloat() method in Numbers :

We use parseFloat() method in order to convert an integer number.

Example:

var nn = Number(45)

console.log(Number.parseFloat(nn))

// Output : 45

(Example : 01)

Example 01 might confuse you. There are no numbers after decimal(.) so compiler simply ignore that and we get integer number inspite of using parseFloat() method.

var nn = Number(‘45.45’)

console.log(Number.parseFloat(nn))

// Output : 45.45

(Example : 02)

In example 02 we use a fractional number and used parseFloat() method. There is a value after decimal(.) so the compiler shows 45.45.

9. Javascript isNAN() method :

‘NAN’ means not a number. isNAN() method determines whether it is NAN.

Example:

var num = Number.isNaN(123)

console.log(num)

// Output: false

(Example : 01)

In example the input value we wrote inside of isNAN() method is number thats why it is false.

var num = Number.isNaN(0/0)

console.log(num)

// Output: true

(Example : 02)

In example 2 (0/0) gives undefined value thats why output is true.

10. Javascript random() method :

We can generate random number by using random() method. But random number generates (0–1) in default.

Example:

var n = 5.249

console.log(Math.random())

// Output : 0.06427917256751181, 0.6778901632871537, 0.7704321614868246

(Example : 01)

If we generate a random number from 1 to 50 then we can write this way :

var n = 5.249

console.log(Math.random() * 50)

// Output : 1.126138172671176, 11.742748387863234, 2.829190995478581,

(Example : 02)

2nd part coming soon………..

Thanks for reading.Comment below your thoughts about this article. cheeers

A Passionate Web technology enthusiast. Curious to know new technology every day.