Javascript – AppDividend https://appdividend.com Latest Code Tutorials Thu, 11 Jul 2019 11:42:29 +0000 en-US hourly 1 https://wordpress.org/?v=5.2.2 https://appdividend.com/wp-content/uploads/2017/08/cropped-ApDivi-32x32.png Javascript – AppDividend https://appdividend.com 32 32 Javascript Constructor Tutorial | Constructor in Javascript Example https://appdividend.com/2019/07/11/javascript-constructor-tutorial-constructor-in-javascript-example/ https://appdividend.com/2019/07/11/javascript-constructor-tutorial-constructor-in-javascript-example/#respond Thu, 11 Jul 2019 11:38:26 +0000 http://localhost/wordpress/?p=9592 Javascript Constructor Tutorial | Constructor in Javascript Example

Javascript Constructor Tutorial | Constructor in Javascript Example is today’s topic. A constructor method is an excellent method for creating and initializing an object produced within the class. There can be only one particular method with a name “constructor” in the class. Having more than one occurrence of the constructor method in the class will […]

The post Javascript Constructor Tutorial | Constructor in Javascript Example appeared first on AppDividend.

]]>
Javascript Constructor Tutorial | Constructor in Javascript Example

Javascript Constructor Tutorial | Constructor in Javascript Example is today’s topic. A constructor method is an excellent method for creating and initializing an object produced within the class.

There can be only one particular method with a name “constructor” in the class. Having more than one occurrence of the constructor method in the class will throw the SyntaxError error.

The constructor can use a super keyword to call a constructor of the parent class.

If you do not specify a constructor method, a default constructor is used.

Javascript Constructor Tutorial

It is considered the good practice to name the constructor functions with an upper-case first letter.

The way to create the “object type,” is to use an object constructor function.

In the example above, function Investor() is the object constructor function.

Objects of the same type are created by calling a constructor function with a new keyword.

let st2 = new Investor("Eleven", 15);
let st3 = new Investor("Robin", 21);

#The this Keyword

In JavaScript, this is the object that “owns” the code.

The value of this, when used in the object, is an object itself.

In the constructor function, this does not have a value. It is the substitute for the new object. The value of this will become a new object when a new the object is created.

One thing you can note that here is that this is not a variable. It is a keyword. You cannot change the value of this.

#Adding a Property to an Object

Adding the new property to an existing object is easy. See the following code.

function Investor(name, age) {
  this.name = name;
  this.age = age;
}

let st2 = new Investor("Eleven", 15);
let st3 = new Investor("Robin", 21);

st2.nationality = 'British';
console.log(st2);

See the output.

➜  es git:(master) ✗ node app
Investor { name: 'Eleven', age: 15, nationality: 'British' }
➜  es git:(master) ✗

#Adding a Method to an Object

Adding the new method to an existing object is easy. See the following code.

function Investor(name, age) {
  this.name = name;
  this.age = age;
}

let st2 = new Investor("Eleven", 15);
let st3 = new Investor("Robin", 21);

st2.nationality = 'British';

st2.eleven = function () {
  return this.name +' is '+ this.age + ' years old';
}
console.log(st2.eleven());

See the following output.

➜  es git:(master) ✗ node app
Eleven is 15 years old
➜  es git:(master) ✗

#Adding a Method to a Constructor

You can also define the constructor methods inside the constructor function.

See the following code example.

function Investor(name, age) {
  this.name = name;
  this.age = age;
  this.eleven = function () {
    return this.name +' is '+ this.age + ' years old';
  }
}

let st2 = new Investor("Eleven", 15);
let st3 = new Investor("Robin", 21);

st2.nationality = 'British';
console.log(st2.eleven());

See the following output.

➜  es git:(master) ✗ node app
Eleven is 15 years old
➜  es git:(master) ✗

You cannot add the new method to an object constructor the same way you add the new method to the existing object. Adding new methods to an object constructor must be done inside a constructor function.

#Built-in JavaScript Constructors

JavaScript has built-in constructors for native objects like the following.

let a1 = new Object();
let a2 = new String();
let a3 = new Number();
let a4 = new Boolean();
let a5 = new Array();
let a6 = new RegExp();
let a7 = new Function();
let a8 = new Date();

As you can see above, the JavaScript has object versions of a primitive data types String, Number, Array, and Boolean. But there is no reason to create the complex objects. Primitive values are much faster.

#Constructor in JavaScript with ES6

In ES6, we can create the classes. If you’ve come from the OOP language like Python or PHP, this will be familiar to you. Look at the following code.

class StrangerThings {
  constructor(name, age, networth) {
    this.name = name;
    this.age = age;
    this.networth = networth;
  }

  get detail() {
    return this.name + ' ' + this.age +' '+ this.networth;
  }
}
const eleven = new StrangerThings("Millie", 15, 4);
console.log(eleven);

See the following output.

➜  es git:(master) ✗ node app
StrangerThings { name: 'Millie', age: 15, networth: 4 }
➜  es git:(master) ✗

The class function creates the template that we can use to create the objects later. The constructor() method is the special method called when the instance of a StrangerThings class is created.

  1. A function keyword is replaced with the class keyword
  2. There’s a particular function named ‘constructor’ where the initialization of an object is done. Hence whatever is the present in the body of a traditional function gets shifted to a ‘constructor’ function. It is called whenever a new object of that ‘class’ is created.
  3. Any method created inside the class body and it is considered a ‘member’ of the class.

#The super and extends keywords

You can create the subclass or a child class using the ‘extends’ keyword. The subclass can add its logic along with the data inherited from the ‘base’ class.

See the following code example.

class Millie {
  constructor(name) {
    this.name = name;
  }
  speak() {
    console.log(this.name + ' makes a noise.');
  }
}
class Eleven extends Millie {
  constructor(character, power = 'telekenesis') {
    super(character);
    this.power = power;
  }
  speak(){
    console.log('UK Accent');
  }
}
let el = new Eleven('Friends Don\'t lie');
el.speak();

See the following output.

➜  es git:(master) ✗ node app
El has UK Accent
➜  es git:(master) ✗

Finally, Javascript Constructor Tutorial | Constructor in Javascript Example is over.

The post Javascript Constructor Tutorial | Constructor in Javascript Example appeared first on AppDividend.

]]>
https://appdividend.com/2019/07/11/javascript-constructor-tutorial-constructor-in-javascript-example/feed/ 0
Javascript Array isArray Tutorial With Example | Array.isArray() Function https://appdividend.com/2019/07/08/javascript-array-isarray-tutorial-with-example-array-isarray-function/ https://appdividend.com/2019/07/08/javascript-array-isarray-tutorial-with-example-array-isarray-function/#respond Mon, 08 Jul 2019 11:35:17 +0000 http://localhost/wordpress/?p=9442 Javascript Array isArray Tutorial With Example | Array.isArray() Function

Javascript Array isArray Tutorial With Example is today’s topic. The Array.isArray() method determines whether the passed value is an Array. If the value is an Array, then true is returned; otherwise, false is return. When checking for the Array instance, the Array.isArray is preferred over the instanceof because it works through iframes. Javascript Array isArray […]

The post Javascript Array isArray Tutorial With Example | Array.isArray() Function appeared first on AppDividend.

]]>
Javascript Array isArray Tutorial With Example | Array.isArray() Function

Javascript Array isArray Tutorial With Example is today’s topic. The Array.isArray() method determines whether the passed value is an Array. If the value is an Array, then true is returned; otherwise, false is return. When checking for the Array instance, the Array.isArray is preferred over the instanceof because it works through iframes.

Javascript Array isArray

See the syntax of isArray() function.

Array.isArray(value)

The value to be checked.

See the following example.

// app.js

let arr = ['Millie', 'Bobby', 'Brown'];
console.log(Array.isArray(arr))

let data = {
  realname: 'Millie Bobby Brown',
  character: 'Eleven',
  series: 'Stranger Things'
};
console.log(Array.isArray(data));

In the above example, we have defined an array of Strings and an array of Objects.

See the following output.

➜  es git:(master) ✗ node app
true
false
➜  es git:(master) ✗

#Javascript isArray behavior with different types

TYPE VALUE AS JSON ARRAY.ISARRAY
Empty Array [] true
An array of Number literal [1,2,3] true
An array of String literal [“a” “b”, “c”] true
Array (using constructor.from) [null,null,null,null,null,null,null,null,null,null] true
Array with empty values using constructor [null,null,null,null,null,null,null,null,null,null] true
Array with values using constructor [1,2,3] true
Array prototype [] true
Array from another window [1,2,3] true
Empty Object {} false
Object with .length property {“length”:10} false
Object whose __prototype__ is Array {} false
Object {“a”: “2”, “b”: “c”} false
Object with nested Array {“nested”:[“2”, “c”]} false
Empty Set {} false
Set of String {} false
Set of Number {} false
Empty Map {} false
Map with different types {} false
String literal “hello world.” false
String Object “hello world” false
Boolean true literal true false
Boolean false literal false false
Explicitly undefined undefined false
Implicit undefined undefined false
null null false
Number (integer) 123 false
Number (float) 1.23 false

An array can be checked if it is empty by using the array.length property. This property returns several elements in the array.

If the number is greater than 0, it evaluates to true.

This method and property can be both used together with the AND(&&) operator to determine whether the array exists and is not empty.

Array isArray() Polyfill

Running the following code before any other code will create Array.isArray() if it’s not natively available in your browser.

if (!Array.isArray) {
  Array.isArray = function(arg) {
    return Object.prototype.toString.call(arg) === '[object Array]';
  };
}

Finally, Javascript Array isArray Tutorial With Example | Array.isArray() Function is over.

The post Javascript Array isArray Tutorial With Example | Array.isArray() Function appeared first on AppDividend.

]]>
https://appdividend.com/2019/07/08/javascript-array-isarray-tutorial-with-example-array-isarray-function/feed/ 0
Javascript Instanceof Operator Example | Instanceof in Javascript https://appdividend.com/2019/07/06/javascript-instanceof-operator-example-instanceof-in-javascript/ https://appdividend.com/2019/07/06/javascript-instanceof-operator-example-instanceof-in-javascript/#respond Sat, 06 Jul 2019 04:36:29 +0000 http://localhost/wordpress/?p=9057 Javascript Instanceof Operator Example | Instanceof in Javascript

Javascript Instanceof Operator Example | Instanceof in Javascript is today’s topic. The instanceof operator tests whether a prototype property of the constructor appears anywhere in the prototype chain of an object. The instanceof operator examines the presence of constructor.type in the object‘s prototype chain. The instanceof operator returns the boolean value that indicates if an object […]

The post Javascript Instanceof Operator Example | Instanceof in Javascript appeared first on AppDividend.

]]>
Javascript Instanceof Operator Example | Instanceof in Javascript

Javascript Instanceof Operator Example | Instanceof in Javascript is today’s topic. The instanceof operator tests whether a prototype property of the constructor appears anywhere in the prototype chain of an object. The instanceof operator examines the presence of constructor.type in the object‘s prototype chain. The instanceof operator returns the boolean value that indicates if an object is an instance of the particular class.

Javascript Instanceof Operator

The instanceof operator is used to check the type of the object at a run time. The instanceof operator tests a presence of constructor.prototype in the object‘s prototype chain.

It checks the current object and returns true if the object is of the specified object type.

The Left Hand Side (LHS) operand is an actual object being tested to the Right Hand Side (RHS) operand, which is an actual constructor of a class.

See the following syntax.

object instanceof constructor

The object is to be tested, and the constructor is the function to test against.

See the following example.

let str = 'The website is appdividend'; 
let myString  = new String();
let nStr    = new String('String created with constructor');
let mDate    = new Date();
let mObj     = {};
let mNonObj = Object.create(null);

console.log(str instanceof String)
console.log(myString instanceof String)
console.log(nStr instanceof String)
console.log(mDate instanceof Date)
console.log(mObj instanceof Object)
console.log(mNonObj instanceof Object)

See the following output.

➜  es git:(master) ✗ node app
false
true
true
true
true
false

In the above example, you can see that if we have used the constructor, then it returns true otherwise false. One thing worth mentioning is that instanceof evaluates to true value if the object inherits from the class’s prototype.

Also, as you have already noticed, String(value) and new String(value) do the totally different things: If you call the constructor function of the built-in types without using the new operator, they try to convert the (‘cast’) an argument to a specific type. If you use the new operator, they create the wrapper object.

See the below code.

let e = new Employee("Eleven");
e instanceof Employee

That is e instanceof Employee is true since e inherits from Employee.prototype.

#Function Prototype

Let’s see another example.

// app.js

function App(name, price, size) {
  this.name = name;
  this.price = price;
  this.size = size;
}
let oz = new App('oz', 15, 1024)
console.log(oz instanceof App);
console.log(oz instanceof Object);

In the above example, we have created a function Prototype called App, which will act as a constructor, and that is why it returns true when we create an object from the function prototype.

When es6 was not there, this is how you need to create a kind of class. See the following output.

➜  es git:(master) ✗ node app
true
true

Every object in the JavaScript has a prototype, accessible through a __proto__ property. Functions also have the prototype property, which is an initial __proto__ for the objects created by them. When the function is created, it is given the unique object for the prototype. An instanceof operator uses this uniqueness to provide you with an answer.

It’s worth noting that instanceof is defined by the use of the “new” keyword when declaring the object.

Specifying “new” actually copied the end state of a String constructor function into the variable, rather than just setting it to the return value. I think this better shows what the new keyword does.

#instanceof Array Built-in Functions

See the following example of Array.

let arr = [1, 2, 3];

console.log(arr instanceof Array);
console.log(arr instanceof Object);

See the following output.

➜  es git:(master) ✗ node app
true
true

Please note that the arr also belongs to the Object class. That’s because Array prototypically inherits from Object.

#Javascript instanceof number

Strings and numbers are the primitive datatypes, not the objects and arrays. Therefore they don’t have a [[Prototype]], so it’ll only work if you wrap them in the regular objects (called ‘boxing’ in Java).

That is why if we check on the number, then it will return false. See the following example.

let num = 21

console.log(num instanceof Number);

See the following output.

➜  es git:(master) ✗ node app
false

For primitives, it will return their type in lower case, for objects, it will return their class in title case.

If you don’t want to discern between primitive values and wrapper objects (for whatever, probably bad reason), use typeof() function.

Finally, Javascript Instanceof Operator Example | Instanceof in Javascript is over.

The post Javascript Instanceof Operator Example | Instanceof in Javascript appeared first on AppDividend.

]]>
https://appdividend.com/2019/07/06/javascript-instanceof-operator-example-instanceof-in-javascript/feed/ 0
Javascript Array copyWithin Example | JS Array copyWithin Tutorial https://appdividend.com/2019/06/20/javascript-array-copywithin-example-js-array-copywithin-tutorial/ https://appdividend.com/2019/06/20/javascript-array-copywithin-example-js-array-copywithin-tutorial/#respond Thu, 20 Jun 2019 11:25:13 +0000 http://localhost/wordpress/?p=8737 Javascript Array copyWithin Example | JS Array copyWithin Tutorial

Javascript Array copyWithin Example | JS Array copyWithin Tutorial is today’s topic. The copyWithin() method copies part of an array to the same array and returns it, without modifying its size i.e, copies array element of an array within the same array. The copyWithin() method copies array elements to another position in the array, overwriting […]

The post Javascript Array copyWithin Example | JS Array copyWithin Tutorial appeared first on AppDividend.

]]>
Javascript Array copyWithin Example | JS Array copyWithin Tutorial

Javascript Array copyWithin Example | JS Array copyWithin Tutorial is today’s topic. The copyWithin() method copies part of an array to the same array and returns it, without modifying its size i.e, copies array element of an array within the same array. The copyWithin() method copies array elements to another position in the array, overwriting the existing values. The copyWithin() method will never add more items to the array. The array copyWithin method overwrites the original array.

Javascript Array copyWithin Example

The copyWithin() works like a C and C++’s memmove and is the high-performance method to shift a data of an Array. It especially applies to a TypedArray method of a same name. The sequence is copied and pasted as only one operation; pasted sequence will have a copied values even when a copy and paste region overlap.

The copyWithin() function is intentionally generic, and it does not require that its this value be the Array object.

The copyWithin() method is the mutable method. It does not modify the length of this, but it will change its content and create the new properties, if necessary.

See the following syntax of copyWithin() method.

array.copyWithin(target, start, end)

The target parameter is required, and it is the index position to copy the elements to.

The start parameter is optional and the index position to start copying elements from.

The end parameter is optional, and the index position to stop copying elements from (default is array.length).

See the following code example.

// app.js

let shows = ["Pink Panther", "Chhota Bheem", "Oggy", "Tom and Jerry", "Doraemon"];
console.log(shows.copyWithin(2, 0, 2));

See the following output.

 

Javascript Array copyWithin Example

Now, see more examples.

console.log([21, 19, 46, 4, 5].copyWithin(-2));

console.log([21, 19, 46, 4, 5].copyWithin(0, 3));

console.log([21, 19, 46, 4, 5].copyWithin(0, 3, 4));

See the following output.

 

JS Array copyWithin Tutorial

So, the JavaScript array copyWithin() method copies the part of the given array with its elements and returns the modified array. This method doesn’t change the length of the modified array.

Finally, Javascript Array copyWithin Example | JS Array copyWithin Tutorial is over.

The post Javascript Array copyWithin Example | JS Array copyWithin Tutorial appeared first on AppDividend.

]]>
https://appdividend.com/2019/06/20/javascript-array-copywithin-example-js-array-copywithin-tutorial/feed/ 0
JavaScript Array Reverse Example | JS Array Reverse() Method https://appdividend.com/2019/06/20/javascript-array-reverse-example-js-array-reverse-method/ https://appdividend.com/2019/06/20/javascript-array-reverse-example-js-array-reverse-method/#respond Thu, 20 Jun 2019 10:08:28 +0000 http://localhost/wordpress/?p=8725 JavaScript Array Reverse Example | JS Array Reverse() Method

JavaScript Array Reverse Example | JS Array Reverse() Method is today’s topic. JS reverse() method reverses the order of the items in an array. The Javascript array reverse() method will change the original array. The function returns an array which represents the array after it has been reversed. The reverse function does not take any […]

The post JavaScript Array Reverse Example | JS Array Reverse() Method appeared first on AppDividend.

]]>
JavaScript Array Reverse Example | JS Array Reverse() Method

JavaScript Array Reverse Example | JS Array Reverse() Method is today’s topic. JS reverse() method reverses the order of the items in an array. The Javascript array reverse() method will change the original array. The function returns an array which represents the array after it has been reversed. The reverse function does not take any argument.

JavaScript Array Reverse Example

The first array item becomes the last, and the last array element becomes the first due to array reverse() method in Javascript. The reverse method transposes the items of the calling array object in place, mutating the array, and returning a reference to the array.

The reverse() is an intentionally generic method that can be called or applied to objects resembling arrays. Objects which do not contain the length property reflecting the last in the series of consecutive, zero-based numerical properties may not behave in any meaningful manner.

Now, See the following syntax of the method.

array.reverse()

#Reversing the elements in an array

See the following code example.

let shows = ["Pink Panther", "Chhota Bheem", "Ben 10", "Tom and Jerry", "Doraemon"];
let output = shows.reverse();
console.log(output);

See the following output.

 

JavaScript Array Reverse Example

#Reversing the elements in an array-like object

The following example creates an array-like object obj, containing three elements and a length property, then reverses the array-like object. The call to reverse() returns the reference to the reversed array-like object obj.

// app.js

const obj = {0: 19, 1: 21, length: 3};

console.log(obj);

Array.prototype.reverse.call(obj);

console.log(obj);

See the following output.

Reversing the elements in an array-like object

 

So, this is how you can reverse the items of an array.

The array reverse() function will reverse your array but modifying the original. If you don’t want to alter the original array then you can do this:

function reverseArr(input) {
  var ret = new Array;
  for(var i = input.length-1; i >= 0; i--) {
      ret.push(input[i]);
  }
  return ret;
}

let arr = ["Pink Panther", "Chhota Bheem", "Ben 10", "Tom and Jerry", "Doraemon"]
let op = reverseArr(arr);
console.log(op);

See the following output.

 

array reverse() function

Finally, JS Array Reverse() Method Tutorial With Example is over.

The post JavaScript Array Reverse Example | JS Array Reverse() Method appeared first on AppDividend.

]]>
https://appdividend.com/2019/06/20/javascript-array-reverse-example-js-array-reverse-method/feed/ 0
Javascript Array lastIndexOf Tutorial | JS Array lastIndexOf() Example https://appdividend.com/2019/06/20/javascript-array-lastindexof-tutorial-js-array-lastindexof-example/ https://appdividend.com/2019/06/20/javascript-array-lastindexof-tutorial-js-array-lastindexof-example/#respond Thu, 20 Jun 2019 09:10:15 +0000 http://localhost/wordpress/?p=8713 Javascript Array lastIndexOf Tutorial | JS Array lastIndexOf() Example

Javascript Array lastIndexOf Tutorial | Array.prototype.lastIndexOf() Example is today’s topic. The lastIndexOf() method searches an array for the specified element and returns its position. The search will start backward at the specified position, or the end if no start position is specified, and end the search at the beginning of the array. It returns -1 […]

The post Javascript Array lastIndexOf Tutorial | JS Array lastIndexOf() Example appeared first on AppDividend.

]]>
Javascript Array lastIndexOf Tutorial | JS Array lastIndexOf() Example

Javascript Array lastIndexOf Tutorial | Array.prototype.lastIndexOf() Example is today’s topic. The lastIndexOf() method searches an array for the specified element and returns its position. The search will start backward at the specified position, or the end if no start position is specified, and end the search at the beginning of the array. It returns -1 if an element is not found. If you want to search from the start to end, use the indexOf() method.

Javascript Array lastIndexOf Tutorial

The lastIndexOf compares searchElement to elements of the Array using strict equality (the same method used by the ===, or triple-equals, operator).

The array lastIndexOf() syntax is following.

array.lastIndexOf(item, index)

The item parameter is required, and it is the item to search for.

The index parameter is optional, and it is index at which to start searching backward. Defaults to the array’s length, i.e., the whole array will be searched. If the index is greater than or equal to the length of the array, the whole array will be searched. If the negative argument is passed then, it is taken as the offset from the end of the array.

The lastIndexOf() function returns the index of the found element from the last.

See the following code example.

// app.js

let shows = ["GoT", "Chernobyl", "Breaking Bad", "Suits", "Friends"];
let output = shows.lastIndexOf("Suits");
console.log(output);

See the output.

 

Javascript Array lastIndexOf Tutorial

If an element to search for is present more than once, the lastIndexOf method returns the position of the last occurrence.

See the following example.

// app.js

let shows = ["GoT", "Chernobyl", "GoT", "Suits", "Friends", "GoT", "Friends"];
let output = shows.lastIndexOf("Friends", 6);
console.log(output);

The length of the show’s array is 6.

Now, in the lastIndexOf() method, we have passed the start index which is 6. So it will start searching backward from array index 6 and if the element is found then its last index will be in the output.

In the above code, we are searching Friends string from the 6th index and also the 6th item is Friends so, the output will be 6 of the above example.

 

JS Array lastIndexOf() Example

#Pass the negative index

Okay, now negative index count from backward means the last element’s index will be -1 and the second last element’s index will be -2 and so on.

See the following example.

// app.js

let shows = ["GoT", "Chernobyl", "GoT", "Suits", "Friends", "GoT", "Friends"];
let output = shows.lastIndexOf("GoT", -4);
console.log(output);

In the above code, we are searching from the end of the array. So, -4th element will be “Suits” and we are searching for GoT.

Now, the only 0th and 2nd position got the GoT and the last index of term GoT is 2 counting from left to right which is 2. So the output will be 2.

 

Pass the negative index

Different from the indexOf method, the lastIndexOf() method searches for the element backward, starting at index.

The above method is the JavaScript extension to the ECMA-262 standard; as such it may not be present on other implementations of the standard. If we want to make it work, we need to add the Polyfills.

Finally, Javascript Array lastIndexOf Tutorial | JS Array lastIndexOf() Example is over.

The post Javascript Array lastIndexOf Tutorial | JS Array lastIndexOf() Example appeared first on AppDividend.

]]>
https://appdividend.com/2019/06/20/javascript-array-lastindexof-tutorial-js-array-lastindexof-example/feed/ 0
Javascript String ToLocaleUpperCase() Tutorial With Example https://appdividend.com/2019/06/18/javascript-string-tolocaleuppercase-tutorial-with-example/ https://appdividend.com/2019/06/18/javascript-string-tolocaleuppercase-tutorial-with-example/#respond Tue, 18 Jun 2019 11:33:56 +0000 http://localhost/wordpress/?p=8610 Javascript String ToLocaleUpperCase() Tutorial With Example

Javascript String ToLocaleUpperCase() Tutorial With Example is today’s topic. In the JavaScript language, the toLocaleUpperCase() method is the string method that is used to convert the string into the uppercase based on locale. Because the toLocaleUpperCase() method is the method of a String object, it must be invoked through the particular instance of the String […]

The post Javascript String ToLocaleUpperCase() Tutorial With Example appeared first on AppDividend.

]]>
Javascript String ToLocaleUpperCase() Tutorial With Example

Javascript String ToLocaleUpperCase() Tutorial With Example is today’s topic. In the JavaScript language, the toLocaleUpperCase() method is the string method that is used to convert the string into the uppercase based on locale. Because the toLocaleUpperCase() method is the method of a String object, it must be invoked through the particular instance of the String class. We have already covered a Javascript toLocaleLowerCase().

Javascript String ToLocaleUpperCase() Tutorial

In JavaScript, the syntax for the toLocaleUpperCase() method is following.

string.toLocaleUpperCase([locale]);

The locale parameter is optional and indicates a locale to be used to convert to the upper case according to any locale-specific case mappings if the multiple locales are given in an Array.

See the following code example.

// app.js

let data = 'brōkən';
console.log(data.toLocaleUpperCase('en-US'));

See the following output.

Javascript String ToLocaleUpperCase() Tutorial

 

The toLocaleUpperCase() method returns a value of the string converted to the upper case according to any locale-specific case mappings. The toLocaleUpperCase() does not affect the value of a string itself. In the most cases, it will produce the same outcome as toUpperCase(), but for some locales, such as the Turkish, whose case mappings do not follow a default case mappings in the Unicode, there may be a different outcome.

Also notice that conversion is not necessarily the 1:1 character mapping, as some of the characters might result in the two or even more characters when transformed to the upper-case. Therefore a length of the result string can differ from an input length. This also implies that the conversion is not stable, for example, the following code can return the false:

x.toLocaleLowerCase() === x.toLocaleUpperCase().toLocaleLowerCase()

See the following more examples.

// app.js

console.log('appdividend'.toLocaleUpperCase());

console.log('appäß'.toLocaleUpperCase());

console.log('i\u0307'.toLocaleUpperCase('lt-LT'));

let locales = ['lt', 'LT', 'lt-LT', 'lt-u-co-phonebk', 'lt-x-lietuva'];
console.log('i\u0307'.toLocaleUpperCase(locales));

See the following output.

 

String ToLocaleUpperCase() Tutorial With Example

Finally, Javascript String ToLocaleUpperCase() Tutorial With Example is over.

The post Javascript String ToLocaleUpperCase() Tutorial With Example appeared first on AppDividend.

]]>
https://appdividend.com/2019/06/18/javascript-string-tolocaleuppercase-tutorial-with-example/feed/ 0
Javascript String toLocaleLowerCase() Tutorial With Example https://appdividend.com/2019/06/18/javascript-string-tolocalelowercase-tutorial-with-example/ https://appdividend.com/2019/06/18/javascript-string-tolocalelowercase-tutorial-with-example/#respond Tue, 18 Jun 2019 10:57:59 +0000 http://localhost/wordpress/?p=8602 Javascript String toLocaleLowerCase() Tutorial With Example

Javascript String toLocaleLowerCase() Tutorial With Example is today’s topic. The toLocaleLowerCase() method converts a string to lowercase letters, according to the host’s current locale. For most languages, it returns the same outcome as toLowerCase function. The toLocaleLowerCase() method returns a calling string value converted to the lower case, according to any locale-specific case mappings. Javascript String toLocaleLowerCase() […]

The post Javascript String toLocaleLowerCase() Tutorial With Example appeared first on AppDividend.

]]>
Javascript String toLocaleLowerCase() Tutorial With Example

Javascript String toLocaleLowerCase() Tutorial With Example is today’s topic. The toLocaleLowerCase() method converts a string to lowercase letters, according to the host’s current locale.

For most languages, it returns the same outcome as toLowerCase function. The toLocaleLowerCase() method returns a calling string value converted to the lower case, according to any locale-specific case mappings.

Javascript String toLocaleLowerCase() Tutorial

The locale is based on the language settings of the browser. The toLocaleLowerCase() method returns the same result as the toLowerCase() method. However, for some locales, where language conflict with the regular Unicode case mappings occurs (such as Turkish), the results may vary. Javascript toLocaleLowerCase() method does not change the original string.

#Syntax

The syntax of string toLowerCase() function is following.

string.toLocaleLowerCase(locale)

The locale parameter indicates a locale to be used to convert to the lower case according to any locale-specific case mappings. If the multiple locales are given in the Array, the best available locale is used.

The toLocaleLowerCase function returns the String, representing the value of a string converted to lowercase according to the host’s current locale.

See the following example.

// app.js

let dottedData = 'Türkçe';
console.log('EN-US: ' + dottedData.toLocaleLowerCase('en-US'));
console.log('TR: ' + dottedData.toLocaleLowerCase('tr'));

See the following output.

Javascript String toLocaleLowerCase() Tutorial

 

See other examples.

console.log('KRUNAL'.toLocaleLowerCase());
console.log('\u0130'.toLocaleLowerCase('tr') === 'i');
console.log('\u0130'.toLocaleLowerCase('en-US') === 'i');
let locales = ['tr', 'TR', 'tr-TR', 'tr-u-co-search', 'tr-x-turkish'];
console.log('\u0130'.toLocaleLowerCase(locales) === 'i');

See the following output.

String toLocaleLowerCase()

 

Finally, Javascript String toLocaleLowerCase() Tutorial With Example is over.

The post Javascript String toLocaleLowerCase() Tutorial With Example appeared first on AppDividend.

]]>
https://appdividend.com/2019/06/18/javascript-string-tolocalelowercase-tutorial-with-example/feed/ 0
Javascript String valueOf Tutorial With Example | String.prototype.valueof() https://appdividend.com/2019/06/18/javascript-string-valueof-tutorial-with-example-string-prototype-valueof/ https://appdividend.com/2019/06/18/javascript-string-valueof-tutorial-with-example-string-prototype-valueof/#respond Tue, 18 Jun 2019 07:41:48 +0000 http://localhost/wordpress/?p=8584 Javascript String valueOf Tutorial With Example | String.prototype.valueof()

Javascript String valueOf Example | String.prototype.valueof() Tutorial is today’s topic. The string.valueOf() is the inbuilt function in JavaScript which is used to return a value of the given string. JS valueOf() method returns a primitive value of the String object. The valueof() in JS method is usually called automatically by JavaScript behind the scenes, and […]

The post Javascript String valueOf Tutorial With Example | String.prototype.valueof() appeared first on AppDividend.

]]>
Javascript String valueOf Tutorial With Example | String.prototype.valueof()

Javascript String valueOf Example | String.prototype.valueof() Tutorial is today’s topic. The string.valueOf() is the inbuilt function in JavaScript which is used to return a value of the given string. JS valueOf() method returns a primitive value of the String object. The valueof() in JS method is usually called automatically by JavaScript behind the scenes, and not explicitly in code.

Javascript String valueOf Tutorial

The string represents the primitive value of a given String object. The JS valueof() returns the primitive value of a String object. See the following syntax of Javascript valueOf() function.

#Syntax

string.valueOf()

The function does not take any parameters.

See the following example.

// app.js

let data = "Krunal Lathiya";
let res = data.valueOf();
console.log(res);

See the output.

Javascript String valueOf Tutorial

 

The valueOf() method of String returns a primitive value of the String object as a string data type. This value is equivalent to String.proptotype.toString method.

Let’s see one more example to print the primitive value of the string object.

// app.js

let data = new String('Ankit Lathiya');
let res = data.valueOf();
console.log(res);

See the following output.

String.prototype.valueof()

 

The Javascript valueOf() method does not change the value of the original string.

Finally, Javascript String valueOf Tutorial With Example | String.prototype.valueof() is over.

The post Javascript String valueOf Tutorial With Example | String.prototype.valueof() appeared first on AppDividend.

]]>
https://appdividend.com/2019/06/18/javascript-string-valueof-tutorial-with-example-string-prototype-valueof/feed/ 0
Javascript String lastIndexOf Example | String.Prototype.lastIndexOf() Tutorial https://appdividend.com/2019/06/18/javascript-string-lastindexof-example-string-prototype-lastindexof-tutorial/ https://appdividend.com/2019/06/18/javascript-string-lastindexof-example-string-prototype-lastindexof-tutorial/#respond Tue, 18 Jun 2019 06:08:48 +0000 http://localhost/wordpress/?p=8571 Javascript String lastIndexOf Example | String.Prototype.lastIndexOf() Tutorial

Javascript String lastIndexOf Example | String.Prototype.lastIndexOf() Tutorial is today’s topic. The lastIndexOf() method returns a position of the last occurrence of a specified value in the string. The string is searched from an end to the beginning, but returns an index starting at the beginning, at position 0. This method returns -1 if a value […]

The post Javascript String lastIndexOf Example | String.Prototype.lastIndexOf() Tutorial appeared first on AppDividend.

]]>
Javascript String lastIndexOf Example | String.Prototype.lastIndexOf() Tutorial

Javascript String lastIndexOf Example | String.Prototype.lastIndexOf() Tutorial is today’s topic. The lastIndexOf() method returns a position of the last occurrence of a specified value in the string. The string is searched from an end to the beginning, but returns an index starting at the beginning, at position 0. This method returns -1 if a value to search for never occurs. The lastIndexOf() method is case sensitive!

Javascript String lastIndexOf Example

Javascript String lastIndexOf method returns the index within the calling String object of the last occurrence of the specified value, searching backward from fromIndex. Returns -1 if a value is not found. The syntax of lastIndexOf() function is following.

Syntax

string.lastIndexOf(searchvalue, start)

The searchValue parameter is required, and it is the string we are looking for.

The start parameter is optional, and it is the position where to start the search (searching backward). If omitted, the default value is a length of the string.

See the following code example.

// app.js

let str = 'Frozen II';
let index = str.lastIndexOf('en');
console.log(index);

See the following output.

Javascript String lastIndexOf Example

 

Characters in the string are indexed from the left to right. An index of the first character is 0, and the index of the last character is string.length – 1.

// app.js

console.log('AppDividend'.lastIndexOf('A'));
console.log('AppDividend'.lastIndexOf('D', 2));
console.log('AppDividend'.lastIndexOf('App', 0));
console.log('AppDividend'.lastIndexOf('A'));
console.log('AppDividend'.lastIndexOf('p', -5));
console.log('AppDividend'.lastIndexOf('D', 0));
console.log('AppDividend'.lastIndexOf(''));
console.log('AppDividend'.lastIndexOf('', 2));

See the output.

String.Prototype.lastIndexOf() Tutorial

 

#Case-sensitivity in Javascript String lastIndexOf

The lastIndexOf() method is case sensitive. For example, the following expression returns -1.

// app.js

console.log('AppDividend'.lastIndexOf('a'));

See the following output.

 

Case-sensitivity in Javascript String lastIndexOf

The following code example uses indexOf and lastIndexOf to locate values.

// app.js

let data = 'FROZEN II Will Be A GOOD MOVIE';

console.log('The index of the first w from the beginning is ' + data.indexOf('F'));
console.log('The index of the first w from the end is ' + data.lastIndexOf('W')); 
console.log('The index of "new" from the beginning is ' + data.indexOf('GOOD'));
console.log('The index of "new" from the end is ' + data.lastIndexOf('GOOD'));

See the following output.

String lastIndexOf Example

 

 

Finally, Javascript String lastIndexOf Example | String.Prototype.lastIndexOf() Tutorial is over.

The post Javascript String lastIndexOf Example | String.Prototype.lastIndexOf() Tutorial appeared first on AppDividend.

]]>
https://appdividend.com/2019/06/18/javascript-string-lastindexof-example-string-prototype-lastindexof-tutorial/feed/ 0