Numbers and string method concatenation in javascript

Number Methods

In javascript method and properties contain a primitive value and it treat primitive value as a object when it execute its method and properties.

Basic maths calculation with numbers in javascript

``````const num1=100;
const num2=20;
let val;
//Basic math with numbers
val=num1 + num2;
val=num1 - num2;
val=num1 * num2;
val=num1 / num2;
val=num1 % num2;
console.log(val);``````

These are the basic math operator and with different output as given below

1. So when you add num1 +num2 you will get 120.
2. When you subtract num1 num2 you will get 80.
3. When you multiple num1 *num2 you will get 2000.
4. When you divide num1 / num2 you will get 5
5. When you modulus num1 % num2 you will get 0

This is so basic but still very important operator to perform opertions in javascript.

Math object in javascript

Math is an object it means that it has properties and methods.A property is basically a attribute and method is just a function inside of an object.The math object are used for certain things like round, get the Absolute, Get the power of something, Generate random numbers etc.

``````//Math object
val=Math.PI;
console.log(val);``````

It gives the value of PI that is 3.141592653589793.

``````//Math object
val=Math.E;
console.log(val);``````

It gives the value of E that is 2.718281828459045.

Round:- Round() gives the number to the nearest integer so if you want to round the number in javascript you can use round().

``````//Math object
val=Math.round(4.9);
console.log(val);``````

When you round the value 4.9 you will get 5.

Ceil:- The ceil()  method is used to round the numbers in upwards which is very close to the nearest  interger.Remember if you pass the argument is an interger then the value will not be rounded because value is already a interger.

``````//Math object
val=Math.ceil(5.3);
console.log(val);``````

When you use ceil() to round 5.3 you will get 6 as a output.

Floor:- The floor() method is used to round the number in downwards which is very close to the nearest interger.If you pass the argument which is interger than value will not be round.

``````//Math object
val=Math.floor(8.3);
console.log(val);``````

When you use floor() to round 8.3 you wiil get 8

Sqrt:- The sqrt() method is used to find the square root of a given number .

``````//Math object
val=Math.sqrt(86);
console.log(val);``````

When you use sqrt() for the value 86 you will get 9.273618495495704 as a square root.

Abs:-The abs() method return the absolute value of a specific number.It also return NaN if the given value is not a number and 0 if the value is null .

``````//Math object
val=Math.abs(-86);
console.log(val);``````

The abs() gives the positive value 86 as a output.

Pow:-The pow() method help to give the power to certain value.

``````//Math object
val=Math.pow(8,4);
console.log(val);``````

The pow() gives output 4096 as a power.

Min and Max:-The min() and max() function of javascript are opposite from each other.The min() method is used reduce (decrease) a given value so it is always less than a given value or equal to its borderline.Whereas max() method is return the number with maximum value.

``````//Math object using min() method
val=Math.min(2,77,898,73,62,60,1,90,0);
console.log(val);``````
``````//Math object using max() method
val=Math.max(2,77,898,73,62,60,1,90,0);
console.log(val);``````

In the first code you get the output 0 because 0 is a smallest digit whereas In second code  where we use max we get the output 898 as a highest number .

Note:- If you put negative number in min() method than you get answer in negative because min() method gives you lowest among all the digit.

Random:-The random() method gives you a random number and it keep changing as you reload the page .

``````//Math object
val=Math.random();
console.log(val);``````

If you want random number without decimal you can use floor() and it will give you output between 0 to 20.

``````//Math object
val=Math.floor(Math.random()*20+1);
console.log(val);``````

String and concatenation

In javascript string is a series of charactersit which is either 0 an 1 it  will be text, double quotes, single quotes etc.Whereas concatenation  is a used to add two or more string.It doesnot change the string instead of that it join two string and give new string as in given code.

``````const string1='Riya';
const string2='singh';
let val;
val=string1+string2;
console.log(val);``````

So from the above code we get the output Riyasingh.If you want  that give space between Riyasingh you just concatenate ' ' so shown below.

``````const string1='Riya';
const string2='singh';
let val;
val=string1+ ' '+ string2;
console.log(val);

​``````

Append:-The append() method is used too add the two string or you can say repeat the string .

``````let val;
//Append
val='Sheetal '
val+='Singh'
console.log(val);``````

Here is another example to concatenate sentence.

``````const firstname='Divya';
const lastname='singh';
const age=12;
const profession='student';
let val;
val='Hello, my name is ' + firstname +lastname +' and I am '+ age + ' years old I am a ' + profession;
console.log(val);``````

Escaping:-The escape() function in javascript is used to encode the string it make the string convenient.We use \ so that it escape it code to just a regular character and it takes its power away at ending of string.

``````let val;
//Escaping
val = 'Don\'t touch this book .I can\'t wait to go school';
console.log(val);``````

so here you get the output as Don't touch this book .I can't wait to go school.

Uppercase and lowercase:-The UpperCase() method is used to convert the string to uppercase (capital) letters whereas the LowerCase() is used to convert the string in lowercase (small) letters.

``````const string1='riya singh';
let val;
val=string1.toUpperCase();
console.log(val);``````
``````const string1='RIYA SINGH';
let val;
val=string1.toLowerCase();
console.log(val);``````

Slice:-The slice() method is used to find the position of string. In slice() method the index start with 0 and it work same as substring().

``````const string1='riya';
const string2='singh';
let val;
//slice()
val=string1.slice(0,3);
console.log(val);``````

Here you get the output riyand if you use negative number the indexing is start with last letter.

Split:-In javascript split() method is used to crack a string in array base on a operator.

``````const string1='riya';
const string2='singh';
const string3='Hello my name is Riya singh and i am a student';
let val;
val=string3.split(' ');
console.log(val);``````

Here you get output (11) ["Hello", "my", "name", "is", "Riya", "singh", "and", "i", "am", "a", "student"]  as it convert the string in array.

Charat:-The charat() method specify the index of a string to return the charcter or a value of a given string .Its index start from 0.

``````const string1='riya';
const string2='singh';
let val;
val=string2.charAt('3');
console.log(val);``````

Here you get g as a output because its indexing start from 0 .

Keywords: