JavaScript and some useful tricks

  1. What is JavaScript?

JavaScript is a client-side scripting language, which means the source code is processed by the client’s web browser rather than on the web server. This means JavaScript functions can run after a web page has loaded without communicating with the server. For example, a JavaScript function may check a web form before it is submitted to make sure all the required fields have been filled out. The JavaScript code can produce an error message before any information is actually transmitted to the server.

2. Character Access

There are two ways to access an individual character in a string. The first is the charAt() method:

// charAt methodreturn ‘cat’.charAt(1) // returns “a”

The other way is to treat the string as an array-like object where every character is placed to a numerical index.

// treat the string like an arrayreturn ‘cat’[1] // returns “a”

3. String.prototype.includes().

The includes() method performs a case sensitive search whether a string is found on another string.

// search word and word1 in sentenceconst sentence = ‘I am a lazy boy’;const word = ‘boy’;const word1 = ‘girl’;console.log(sentence.includes(word));console.log(sentence.includes(word1));The output will be :truefalse

4. Difference between trim(), trimStart(), trimEnd().

The trim() method is used to remove whitespace from both sides of the string.

It returns a new string stripped whitespace from both the end and beginning of a string.If no whitespaces are found then it still returns a new string (copy of the existing string) with no exception being thrown.

// code for trim() methodconst str = ‘ Iftekhar Hasan ’;console.log(str.trim());The output will be :“Iftekhar Hasan”

The trimStart() method is used to remove whitespace from the start of the string. It returns a new string stripped whitespace from the start of a string.If no whitespaces are found at the start of a string then it still returns a new string (copy of the existing string) with no exception being thrown.

// code for trimStart() methodconst str = ‘ Iftekhar Hasan ’;console.log(str.trimStart());The output will be :“Iftekhar Hasan ”

The trimEnd() method is used to remove whitespace from the end of the string. It returns a new string stripped whitespace from the end of a string.If no whitespaces are found at the end of a string then it still returns a new string (copy of the existing string) with no exception being thrown.

// code for trimEnd() methodconst str = ‘ Iftekhar Hasan ’;console.log(str.trimEnd());The output will be :“ Iftekhar Hasan”

5. Difference between slice() and split()

The slice() method extracts a portion of a string and returns a new string without modifying the original string.

The split() method divides a string into an ordered list of substrings and puts them into an array. It returns an array of strings.

// Code for slice() methodconst str = ‘The quick brown fox jumps over the lazy dog.’;console.log(str.slice(31));// expected output: “the lazy dog.”console.log(str.slice(4, 19));// expected output: “quick brown fox”console.log(str.slice(-4));// expected output: “dog.”console.log(str.slice(-9, -5));// expected output: “lazy”// code for split() methodconst str = ‘The quick brown fox jumps over the lazy dog.’;const words = str.split(‘ ‘);console.log(words);// expected output: Array [“The”, “quick”, “brown”, “fox”, “jumps”, “over”, “the”, “lazy”, “dog.”]console.log(words[4]);// expected output: “jumps”

6. Number.parseFloat(), Number.parseInt().

The Number.parseFloat() method parses an argument and returns a floating point number. If a number cannot be parsed from the argument, it returns NaN.

// Code for parseFloat() methodconst num = ‘3.1416’;console.log(Number.parseFloat(num));// expected output: 3.1416

The Number.parseInt() method parses a string argument and returns an integer of the specified radix or base. It returns an integer parsed from the given string.

// Code for parseInt() methodconst num = ‘3.1416’;console.log(Number.parseInt(num));// expected output: 3

7. Math.abs()

The Math.abs() function returns the absolute value of a number. That is if a result is negative value and we put it in Math.abs function we will get positive value means absolute value.It doesn’t return negative value.

// code for Math.abs()const a = -3console.log(Math.abs(a));// expected output: 3

8. Difference between ceil(), floor() , min(), max()

The ceil() function always returns a number up to the next largest integer.

The floor() function always returns a largest Integer less than or equal to a given number .

The static function Math.min() returns the lowest-valued number passed into it, or NaN if any parameter isn’t a number and can’t be converted into one.

The Math.max() function returns the largest of the zero or more numbers given as input parameters, or NaN if any parameter isn’t a number and can’t be converted into one

// code for ceil(), floor() , min(), max()console.log(Math.ceil(.95));// expected output: 1console.log(Math.floor(5.95));// expected output: 5console.log(Math.min(2, 3, 1));// expected output: 1console.log(Math.max(1, 3, 2));// expected output: 3

9. Array.prototype.concat()

The concat() method is used to merge two or more arrays. This function not change the existing array but it returns a new array.

Example:

const array1 = [‘a’, ‘b’, ‘c’];const array2 = [‘d’, ‘e’, ‘f’];const array3 = array1.concat(array2);console.log(array3);// expected output: Array [“a”, “b”, “c”, “d”, “e”, “f”]Syntax:concat()concat(value0)concat(value0, value1)concat(value0, value1, … , valueN)

10.Array.prototype.every()

The every() method tests whether all elements in the array pass the test applied by the provided function and returns boolean value.

Example:

const isBelowThreshold = (currentValue) => currentValue < 60;const array1 = [1, 50, 39, 24, 13, 36];console.log(array1.every(isBelowThreshold));// expected output: trueSyntax:every(callbackFn)every(callbackFn, thisArg)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store