Basics of JavaScript
JavaScript is a programming language to create dynamic web sites. JS supports Object oriented programming and functional programming. It is a dynamic typed language, which means we don’t need to declare type of variables. Js variables can be of different types. Such as:
· Numbers
· Strings
· Objects
· Arrays
· Functions
· Boolean
Numbers:
If variable presents in numbers than it called Numbers variable. String or other can be converted to number if value given in Number(value) function.
Number(‘abc’) // output will be Nan
Number(‘456’) // output will be 456
Some properties of Numbers are:
· Number.Max_Value(returns maximum numeric value)
· Number.Min_Value(returns minimum numeric positive value)
· Number.NaN(returns Not a Number)
Some popular methods of numbers are:
· Number.isInteger(5) [check 5 is integer or not]
· Number.parseFloat(‘1.25jhfg’) [parses the argument and return 1.25]
· Number.parseInt(‘1.25jhfg’) [parses the argument and return 1, only integer value, no floating value]
· Number.parseFloat(‘1.35478’).toFixed(1) [parses the argument and return 1.3 , 1 position value after .]
· Number. parseFloat(‘1.35478’).toPrecision(4) [parses the argument and return 1.354, as per precision value ]
Strings:
If variable represents in one or more letters within the quotation, it called strings.
Property of strings:
const check = ‘dgtre sgdju jkuyt’;
console.log(check.length); output will be 15
Some popular methods of strings are:
· charAt(value) method return that alphabet of a string as specified position as value.
const str=”njhtd”;
console.log(str.charAt(2)); // output will be h
· concat() method joins strings to give a new string.
const str1 = ‘Tashfia’;
const str2 = ‘Hoque’;
console.log(str1.concat(str2)); // output: “TashfiaHoque”
console.log(str1.concat(‘ ‘, str2)); // output: “Tashfia Hoque”
· indexOf() method returns the first matched index of a character, if not returns -1.
const p =”cat sat on a mat and rat sat on a cat”;
const find = ‘cat’;
console.log(p.indexOf(find)); // output: 0
· includes() method returns the search item exists or not exist in a string.
const p =”cat sat on a mat and rat sat on a cat”;
const find = ’and’;
console.log(p.includes(find)); // output: true
· lastIndexOf() method returns the last index value if search item has mulitiple position.
const p =’cat sat on a mat and rat sat on a cat’;
const find = ‘cat’;
console.log(p.lastIndexOf(find)); // output: 34
· replace() method returns a new string by replacing old one with matches of a pattern. If pattern is regular expression then it will return only the first matched.
const p =’cat sat on a mat’;
const find = ‘cat’;
console.log(p.replace(find, ‘rat’)); //output: rat sat on a mat