Found 6710 Articles for Javascript

What is the use of Object.is() method in JavaScript?

vineeth.mariserla
Updated on 30-Jul-2019 22:30:26

195 Views

Object.is()Object.is() is used to check whether two values are same or not. Two values are same when they have the following criteria. Either both the values are undefined or null .Either both are true or false.Both strings should be of same length, same characters and in same order.The polarities of both the values should be equal.Both the values can be NaN and should be equal.syntaxObject.is(val1, val2);It accepts two parameters and scrutinize whether they are equal or not. If equal gives out true as output else false as output.There is a small difference between Object.is() and "==" that is when comparing +0 and -0, the former results false whereas the latter results true. ... Read More

How to add two strings with a space in first string in JavaScript?

vineeth.mariserla
Updated on 29-Jun-2020 11:10:23

5K+ Views

To add two strings we need a '+' operator to create some space between the strings, but when the first string itself has a space with in it, there is no need to assign space explicitly. In the following example since the string 'str1' has a space with in it, just only concatenation without space is adequate to add both the strings.ExampleLive Demo           function str(str1, str2) {          return (str1 + str2);       }       document.write(str("tutorix is the best ", "e-learning platform"));     Outputtutorix is the best ... Read More

How to parse an URL in JavaScript?

vineeth.mariserla
Updated on 30-Jul-2019 22:30:26

386 Views

Parsing an URLIt is very simple to parse an URL in javascript by using DOM method rather than Regular expressions. If regular expressions are used then code will be much more complicated. In DOM method just a function call will return the parsed URL. In the following example, initially a function is created and then an anchor tag "a" is created inside it using a DOM method. Later on the provided URL was assigned to the anchor tag using href. Now, when function returns the parts of the URL, it tries to return the parsed parts as shown in the output. ... Read More

How to decode an encoded string in JavaScript?

vineeth.mariserla
Updated on 30-Jul-2019 22:30:26

11K+ Views

DecodingIn JavaScript, to decode a string unescape() method is used. This method takes a string, which is encoded by escape() method, and decodes it. The hexadecimal characters in a string will be replaced by the actual characters they represent using unescape() method.Syntaxunescape(string)ExampleIn the following the two exclamation marks have converted to hexadecimal characters using escape() method. Later on those marks were decoded in to their natural characters using unescape() method. Live Demo    // Special character encoded with escape function    var str = escape("Tutorialspoint!!");    document.write("");    document.write("Encoded : " + str);    // unescape() function    document.write("Decoded ... Read More

How to remove non-word characters in JavaScript?

vineeth.mariserla
Updated on 30-Jul-2019 22:30:26

341 Views

Removing non-word charactersTo remove non-word characters we need to use regular expressions. The logic behind removing non-word characters is that just replace the non-word characters with nothing('').ExampleIn the following example there are many non-word characters and in between them there exists a text named "Tutorix is the best e-learning platform". So using regular expressions the non-word characters were replaced with nothing('') so as to get the word characters as the output.Live Demo    function remNonWord (string) {       if ((string===null) || (string===''))       return false;       else       string = ... Read More

What is the use of weakSet.has() method in JavaScript?

vineeth.mariserla
Updated on 29-Jun-2020 09:49:47

174 Views

weakSet.has()This is an inbuilt function in javascript which is used to return a boolean value on scrutinizing whether an object is present in weakSet or not. The weakSet object lets you store weakly held objects in a collection.SyntaxweakSet.has(obj);ArgumentsFrom the above line of code,  weakSet.has() accepts a parameter 'obj' and checks whether the parameter is present in the provided weakSet or not.Returning valueBased on presence of value, whether it is in weakSet or not, the weakSet.has() method returns a boolean output. If the value is present then true will be returned else false will be returned.Example-1In the following example weakSet.has() checks whether the object(user provided) 'object1' is ... Read More

How to hide e-mail address from an unauthorized user in JavaScript?

vineeth.mariserla
Updated on 30-Jul-2019 22:30:26

1K+ Views

Hiding an e-mail addressThe following steps are to be followed to hide our e-mail from unauthorized users. In every email address '@' symbol is common so try to remove it from the email address using split() method. In the following example after splitting the email(batman@gmail.com) we get the result as batman, gmail.com.Divide the result in to 2 parts(split1 and split2). Using substring() method remove some of string from split1 and join resulted part with split2 using '...@'. Return the joined part as the final output. In our example the resulted output is "bat...@gmail.com".ExampleLive Demo newEmail = function (email) { ... Read More

How to access a function property as a method in JavaScript?

vineeth.mariserla
Updated on 30-Jul-2019 22:30:26

1K+ Views

Accessing a function as a method A javascript object is made up of properties. To access a property as a method, just define a function to a property and include other properties in that function.In the following example an object called "employee" is created with properties "fullName", "lastName" , "firstName" and "id". A function is defined under property "fullName" and properties such as "firstName" and "lastName" were included in it. So when the property "fullName" is called, the full name of the employee is going to display as shown in the output.Example-1Live Demo    var employee = {   ... Read More

How to add a number and a string in JavaScript?

vineeth.mariserla
Updated on 29-Jun-2020 09:44:54

11K+ Views

In javascript , we can add a number and a number but if we try to add a number and a string then, as addition is not possible, 'concatenation' takes place.In the following example, variables a, b, c and d are taken. For variable 'a', two numbers(5, 5) are added therefore it returned a  number(10). But in case of variable 'b' a string and a number ('5', 5) are added therefore, since a string is involved, we get the result as '55', which is a string. since strings are involved, Variables 'c' and 'd' also return a string as shown in the ... Read More

How to count a number of words in given string in JavaScript?

vineeth.mariserla
Updated on 29-Jun-2020 09:34:01

5K+ Views

Using regular expressions it is easy to count number of words in a given string in javascript. There are some steps to be followed to count number of wordsSteps to followWe know that a sentence or a phrase is made up of words that are separated with spaces in between and there are some instances in which the words are separated by 2 or more spaces. A developer must notice all these points while calculating no of words. Step-1Exclude the start and end spaces of a string. The following line of regex expression will remove the start and end spaces of ... Read More

Advertisements