JAVASCRIPT BASIC CONCEPT

JavaScript Tutorial

JavaScript:- String, Number, Math, Array.

Fahim Faysal

--

1. charAt()

charAt() method is used to find out a char from at the specified index in a string, and return a value. The index of the first character is 0, the second character is 1, and so on.

Syntax: string.charAt(index).

Example:

let string = “Hello I am Fahim”;

let result = string.charAt(0)

console.log(result);

Anser : H

2.concat()

The concat() method is used to join two or more strings. It returns new strings, containing the text of the combined strings. But it doesn’t change the original string. If the arguments are not of the typed string, they are converted to string values before concatenating.

Syntax: concat(string1, string2, … ,stringX)

Example:

let string1 = “Learn”;

let string2 = “JavaScript”;

let string3 = “Programming”;

let result = string1.concat(string2, string3);

console.log(result);

Anser : LearnJavaScriptProgrammig

3.substr()

The substr() method extracts parts of a string, beginning at the character at the specified position, and returns the specified number of characters. The index of the first character to include in the returned substring. The substr() method does not change the original string.

Syntax: string.substr(start, length)

Example:

let string = “Hello JavaScript”;

let result = string.substr(1, 2);

console.log(result);

Anser : el

4.Number.isNaN()

The Number.isNaN() method determines whether the passed value is NaN and its type is Number. This method returns true if the value is of the type Number, and equates to NaN. Otherwise, it returns false.Number.isNaN() is different from the global isNaN()

function. The global isNaN() function converts the tested value to a Number, then tests it. In JavaScript, the value NaN is considered a type of number.

Syntax: Number.isNaN(value)

Example:

Number.isNaN(123)

Ans: false

Number.isNaN(-1.23)

Ans:false

Number.isNaN(5–2)

Ans:false

Number.isNaN(0)

Ans:false

Number.isNaN(‘123’)

Ans:false

Number.isNaN(‘Hello’)

Ans:false

Number.isNaN(‘2005/12/12’)

Ans:false

Number.isNaN(‘’)

Ans:false

Number.isNaN(true)

Ans:false

Number.isNaN(undefined)

Ans:false

Number.isNaN(‘NaN’)

Ans:false

Number.isNaN(NaN)

Ans:true

Number.isNaN(0 / 0)

Ans:true

5. Number.parseFloat()

The parseFloat() function parses a string and returns a floating-point number. The value to parse. If this argument is not a string, then it is converted to one using the ToString abstract operation. Leading whitespace in this argument is ignored. Only the first number in the string is returned. If the first character cannot be converted to a number, parseFloat() returns NaN.

Syntax: Number.parseFloat(string)

Example:

let a = parseFloat(“10”);

console.log(a);

Ans: 10

let b = parseFloat(“10.00”);

console.log(b);

Ans: 10

let c = parseFloat(“10.33”);

console.log(c );

Ans: 10.33

let d = parseFloat(“34 45 66”);

console.log(d);

Ans: 34

let e = parseFloat(“ 60 “);

console.log(e);

Ans: 60

let f = parseFloat(“40 years”);

console.log(f);

Ans: 40

let g = parseFloat(“He was 40”);

console.log(g);

Ans: NaN

6.Number.parseInt()

The parseInt() function parses a string and returns an integer. The value to parse. If this argument is not a string, then it is converted to one using the ToString abstract operation. Leading whitespace in this argument is ignored. Only the first number in the string is returned. Leading and trailing spaces are allowed. If the first character cannot be converted to a number, parseInt() returns NaN.

Syntax: Number.parseInt(string) & Number.parseInt(string, radix)

Example:

var a = parseInt(“10”) ;

console.log(a);

Ans: 10

var b = parseInt(“10.00”);

console.log(b);

Ans: 10

var c = parseInt(“10.33”) “;

console.log(c);

Ans: 10

var d = parseInt(“34 45 66”);

console.log(d);

Ans: 34

var e = parseInt(“ 60 “)”;

console.log(e);

Ans: 60

var f = parseInt(“40 years”);

console.log(f);

Ans: 40

var g = parseInt(“He was 40”);

console.log(g);

Ans: NaN

var h = parseInt(“10”, 10);

console.log(h);

Ans: 10

var i = parseInt(“010”);

console.log(i);

Ans: 10

var j = parseInt(“10”, 8);

console.log(j);

Ans: 8

var k = parseInt(“0x10”);

console.log(k);

Ans: 16

var l = parseInt(“10”, 16);

console.log(l);

Ans: 16

7.Math.abs()

The Math.abs() method returns the absolute value of a number. That is, it returns x if x is positive or zero, and the negation of x if x is negative. A Number, representing the absolute value of the specified number, or NaN if the value is not a number, or 0 if the value is null.

Syntax: Math.abs(x)

Example:

Math.abs(-22.6);

Ans : 22.6

8.lastIndexOf()

The Math.ceil() method always rounds a number up to the next largest integer and returns the result. If the passed argument is an integer, the value will not be rounded. But the smallest integer greater than or equal to the given number. A Number, representing the nearest integer when rounding upwards.

Syntax: Math.ceil(x)

Example:

Math.ceil(5.7)

Ans : 6

9.Math.sqrt()

The Math.sqrt() method returns the square root of a number. The square root of the given number. If the number is negative, NaN is returned. Because sqrt() is a static method of Math, you always use it as Math.sqrt(), rather than as a method of a Math object you created (Math is not a constructor).

Syntax: Math.sqrt(x)

Example:

Math.sqrt(25);

Ans : 5

10.Array.prototype.every()

The every() method tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value. every() does not execute the function for array elements without values and also every() does not change the original array. A Boolean. Returns true if all the elements in the array pass the test, otherwise, it returns false.

Syntax: array.every(function(currentValue, index, arr), thisValue)

Example:

let ages = [15, 35, 18, 42];

const checkAdult = (age) => {

return age >= 18;

}

console.log(ages.every(checkAdult));

Ans: false

--

--

Fahim Faysal
0 Followers

Web Developer, Programmer, Cyclist, and Playing Soccer.