JavaScript Substring Example - Slice, Substr, and Substring Methods in JS

Learn how to get a JavaScript substring with an example of slice( ), substring( ) and substr( ) methods in JS.

#javascript #substring methods in js

What is GEEK

Buddha Community

JavaScript Substring Example - Slice, Substr, and Substring Methods in JS

JavaScript Substring Example - Slice, Substr, and Substring Methods in JS

Learn how to get a JavaScript substring with an example of slice( ), substring( ) and substr( ) methods in JS.

#javascript #substring methods in js

Javascript Array Slice Example | Array.prototype.slice()

Javascript array slice() is an inbuilt function that returns the shallow copy of the portion of an array into a new array object selected from beginning to end. The slice() method returns the selected elements in an array, as a new array object. The original array will not be modified. So it is a  pure function. The slice() method selects the elements starting at the given start argument, and ends at, but does not include, the given end argument.

Javascript Array Slice Example

The syntax for the Javascript array slice() method is the following.

array.slice(start, end)

The start parameter is optional, and it is the integer that specifies where to start the selection.

The end parameter is optional, and it is the integer that specifies where to end the selection.

Let us take an example by creating a file called app.js and add the following code.

// app.js

let namepartner = ['Pearson', 'Specter', 'Litt'];
let suits = namepartner.slice(1, 2);

console.log(suits);

Javascript Array Slice Example | Array.prototype.slice() Tutorial

So here, what happens under the hood is that when the .slice() method is called frequently, this keyword works as an Array, and then it iterates over an Array.

#javascript #array.prototype.slice #js #app.js

Javascript String SubString: How to Get SubString in JS

The substring() extracts characters from indexStart up to but not including indexStart in particular. If an indexEnd is omitted, then the substring() extracts characters to the end of a  string. If an indexStart is equal to the indexEnd, then the substring() returns an empty  string.

Javascript String substring()

Javascript string substring() is an inbuilt function that returns a part of the string between the start and end indexes, or to the end of a string.

Syntax

The syntax for the javascript string substring() method is the following.

string.substring(indexStart,indexEnd)

#javascript #javascript string substring #js

Grokking Call(), Apply() and Bind() Methods in JavaScript

These functions are very important for every JavaScript Developer and are used in almost every JavaScript Library or Framework. Check out the code snippet below.

Taken from the very popular library Lodash

/**
	* Creates a function that invokes `func` with arguments reversed.
	*
	* @since 4.0.0
	* @category Function
	* @param {Function} func The function to flip arguments for.
	* @returns {Function} Returns the new flipped function.
	* @see reverse
	* @example
	*
	* const flipped = flip((...args) => args)
	*
	* flipped('a', 'b', 'c', 'd')
	* // => ['d', 'c', 'b', 'a']
	*/
	function flip(func) {
	  if (typeof func !== 'function') {
	    throw new TypeError('Expected a function')
	  }
	  return function(...args) {
	    return func.apply(this, args.reverse())
	  }
	}

	export default flip

Look at the statement on line 21, return func.apply(this, args.reverse())

In this article, we will have a look at the call()apply() and bind() methods of JavaScript. Basically these 3 methods are used to control the invocation of the function. The call() and apply() were introduced in ECMAScript 3 while bind() was added as a part of ECMAScript 5.

Let us start with an example to understand these.

Suppose you are a student of X university and your professor has asked you to create a math library, for an assignment, which calculates the area of a circle.

const calcArea = {
	  pi: 3.14,
	  area: function(r) {
	    return this.pi * r * r;
	  }
	}

calcArea.area(4); // prints 50.24


You test this and verify its result, it is working fine and you upload the library to portal way before the deadline ends. Then you ask your classmates to test and verify as well, you come to know that that your result and their results mismatches the decimals precision. You check the assignment guidelines, Oh no! The professor asked you to use a constant **pi** with 5 decimals precision. But you used **3.14** and not **3.14159** as the value of pi. Now you cannot re-upload the library as the deadline date was already over. In this situation, **call()** function will save you.

#js #javascript-development #javascript #javascript-interview #javascript-tips

Ajay Kapoor

1626321063

JS Development Company India | JavaScript Development Services

PixelCrayons: Our JavaScript web development service offers you a feature-packed & dynamic web application that effectively caters to your business challenges and provide you the best RoI. Our JavaScript web development company works on all major frameworks & libraries like Angular, React, Nodejs, Vue.js, to name a few.

With 15+ years of domain expertise, we have successfully delivered 13800+ projects and have successfully garnered 6800+ happy customers with 97%+ client retention rate.

Looking for professional JavaScript web app development services? We provide custom JavaScript development services applying latest version frameworks and libraries to propel businesses to the next level. Our well-defined and manageable JS development processes are balanced between cost, time and quality along with clear communication.

Our JavaScript development companies offers you strict NDA, 100% money back guarantee and agile/DevOps approach.

#javascript development company #javascript development services #javascript web development #javascript development #javascript web development services #javascript web development company