Found 6685 Articles for Javascript

How to encode a URL using JavaScript function?

Shubham Vora
Updated on 23-Aug-2022 09:12:46

2K+ Views

As we know, a URL is a web address. What is URL encodingand why do we need to encode a URL? The process of turning a string into an accurateURL format is known as URL encoding. A valid URL format only uses "alpha | digit | safe | extra | escape" characters. Only a limited selection of the normal 128 ASCII characters is permitted in URLs. It is required to encrypt reserved characters that are not part of this set. The reliability and security of the URLs are increased with URL encoding. Each character that needs to be URL-encoded is ... Read More

Why should we not use ++, -- operators in JavaScript?

Priya Pallavi
Updated on 19-Jun-2020 13:37:49

137 Views

The increment (++) and decrement (---) operators should be avoided since it can lead to unexpected results. Here are some of the conditions −ExampleIn an assignment statement, it can lead to unfavorable results −Live Demo                                        var a = 5;                    var b = ++a;                    var c = a++;                    var d = ++c;                        document.write(a);                    document.write("\r"+b);                    document.write("\r"+c);                    document.write("\r"+d);                           OutputWhitespace between the operator and variable can also lead to unexpected results −a = b = c = 1; ++a ; b -- ; c;

How to decode a URL using JavaScript function?

Shubham Vora
Updated on 25-Oct-2022 09:06:43

816 Views

In this tutorial, we will learn how to decode a URL using JavaScript. URL is an abbreviation for Uniform Resource Locator. A URL is the address of a certain Web site. Each valid URL, in principle, links to a different resource. These resources include an HTML page, a CSS document, a picture, etc. There are several exceptions in practice, the most frequent being a URL linking to a resource that no longer exists or has migrated. Because the Web server handles both the resource represented by the URL and the URL itself, it is up to the web server's owner ... Read More

How do I convert a string into an integer in JavaScript?

George John
Updated on 07-Jan-2020 11:02:57

1K+ Views

The parseInt() method in JavaScript converts a string into a number. The method returns NaN if the value entered cannot be converted to a number.ExampleYou can try to run the following code to convert a string into an integer − Live Demo           Check                      function display() {             var val1 = parseInt("50") + "";             var val2 = parseInt("52.40") + "";             var val3 = parseInt(" 75 ") + "";             var res = val1 + val2 + val3;             document.getElementById("demo").innerHTML = res;          }          

How to validate a given number in JavaScript?

Shubham Vora
Updated on 15-Nov-2022 09:02:03

4K+ Views

In this tutorial, let us discuss how to validate a given number in JavaScript. Users can follow the syntax below each method to use it. Using regular expression The method tests or matches the regular expression with the input. Syntax /\D/.test(input); input.match(/^[0-9]*$/); If the input is a number, the test returns true. Example The program succeeds in all cases except for "30", [], and "". Number validation using regular expression Validate ... Read More

How to add a number of months to a date using JavaScript?

Paul Richard
Updated on 07-Jan-2020 11:00:57

537 Views

To add a number of months to a date, first get the month using getMonth() method and add a number of months.ExampleYou can try to run the following code to add a number of monthsLive Demo                    var d, e;          d = new Date();          document.write(d);          e = d.getMonth()+1;          document.write("Incremented month = "+e);          

Function Expression vs Function Declaration in JavaScript?

Rishi Raj
Updated on 12-Jun-2020 11:30:56

153 Views

Function DeclarationThe “function” keyword declares a function in JavaScript. To define a function in JavaScript use the “function” keyword, followed by a unique function name, a list of parameters (that might be empty), and a statement block surrounded by curly braces.Here’s an example −function sayHello(name, age) {    document.write (name + " is " + age + " years old."); }Function ExpressionFunction Expression should not start with the keyword “function”. Functions defined can be named or anonymous.Here are the examples −//anonymous function expression var a = function() {    return 5; }Or//named function expression var a = function bar() {    return 5; }

Which equals operator (== vs ===) should be used in JavaScript?

Nancy Den
Updated on 07-Jan-2020 10:59:49

81 Views

Double equals (==) is abstract equality comparison operator, which transforms the operands to the same type before making the comparison. For example,5 ==  5       //true '5' == 5      //true 5 == '5'      //true 0 == false    //trueTriple equals (===) are strict equality comparison operator, which returns false for different types and different content.For example,5 === 5  // true 5 === '5' // false var v1 = {'value':'key'}; var v2 = {'value': 'key'}; v1 === v2 //false

How to parse JSON object in JavaScript?

Vikyath Ram
Updated on 12-Jun-2020 11:28:10

541 Views

ExampleTo parse JSON object in JavaScript, implement the following code −                    myData = JSON.parse('{"event1":{"title":"Employment period","start":"12\/29\/2011 10:20 ","end":"12\/15\/2013 00:00 "},"event2":{"title":"Employment period","start":"12\/14\/2011 10:20 ","end":"12\/18\/2013 00:00 "}}')          myArray = []          for(var e in myData){             var dataCopy = myData[e]             for(key in dataCopy){                if(key == "start" || key == "end"){                   dataCopy[key] = new Date(dataCopy[key])                }             }             myArray.push(dataCopy)          }          document.write(JSON.stringify(myArray));          

What is a ternary operator (?:) in JavaScript?

Swarali Sree
Updated on 07-Jan-2020 10:58:35

464 Views

The conditional operator or ternary operator first evaluates an expression for a true or false value and then executes one of the two given statements depending upon the result of the evaluation.S.NoOperator & Description1? : (Conditional )If Condition is true? Then value X: Otherwise value YExampleYou can try to run the following code to understand how Ternary Operator works in JavaScriptLive Demo                    var a = 10;          var b = 20;          var linebreak = "";          document.write ("((a > b) ? 100 : 200) => ");          result = (a > b) ? 100 : 200;          document.write(result);          document.write(linebreak);          document.write ("((a < b) ? 100 : 200) => ");          result = (a < b) ? 100 : 200;          document.write(result);          document.write(linebreak);          

Advertisements