Javascript Articles

Page 483 of 534

How to create arrays in JavaScript?

Anjana
Anjana
Updated on 15-Jun-2020 493 Views

To create an array in JavaScript, simply assign values −var animals = ["Dog", "Cat", "Tiger"];You can also use the new keyword to create arrays in JavaScript −var animals = new Array("Dog", "Cat", "Tiger");The Array parameter is a list of strings or integers. When you specify a single numeric parameter with the Array constructor, you specify the initial length of the array. The maximum length allowed for an array is 4, 294, 967, 295.ExampleLet’s see an example to create arrays in JavaScriptLive Demo           JavaScript Arrays                   ...

Read More

How to convert a Boolean to Integer in JavaScript?

Ayyan
Ayyan
Updated on 15-Jun-2020 457 Views

Yes, it is possible to convert Boolean to Integer in JavaScript, using the Ternary Operator.ExampleYou can try to run the following code to convert Boolean to Integer −Live Demo           JavaScript Boolean                        var answer = true;          document.write(answer);          var pass_marks = answer ? 90 : 40;          document.write("Passing Marks: "+pass_marks);          

Read More

How to get the first index of an occurrence of the specified value in a string in JavaScript?

Manikanth Mani
Manikanth Mani
Updated on 15-Jun-2020 1K+ Views

To get the first index of an occurrence of the specified value in a string, use the JavaScript indexOf() method.ExampleYou can try to run the following code to get the first index −Live Demo           JavaScript String indexOf() Method                        var str = new String( "Learning is fun! Learning is sharing!" );          var index = str.indexOf( "Learning" );          document.write("First index of string Learning :" + index );          

Read More

How to convert a boolean value to string value in JavaScript?

Alankritha Ammu
Alankritha Ammu
Updated on 15-Jun-2020 16K+ Views

To convert a Boolean value to string value in JavaScript, use the toString() method. This method returns a string of either "true" or "false" depending upon the value of the object.ExampleYou can try to run the following code to convert a boolean value to string value −Live Demo           JavaScript toString() Method                        var flag = new Boolean(true);          document.write( "flag.toString is : " + flag.toString() );          

Read More

How to create a strikethrough text using JavaScript?

Akshaya Akki
Akshaya Akki
Updated on 15-Jun-2020 5K+ Views

To create a strikethrough text with JavaScript, use the strike() method. This method causes a string to be displayed as struck-out text as if it were in a tag.ExampleYou can try to run the following code to create a strikethrough text −Live Demo           JavaScript String strike() Method                        var str = new String("Demo Text");          document.write(str.strike());          alert(str.strike());          

Read More

Why avoid increment (“++”) and decrement (“--”) operators in JavaScript?

Rahul Sharma
Rahul Sharma
Updated on 15-Jun-2020 533 Views

The increment and decrement operators should be avoided since it can lead to unexpected results. Here are some of the conditions:In an assignment statement, it can lead to unfavorable results:ExampleLive 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;

Read More

What is increment (++) operator in JavaScript?

Amit Sharma
Amit Sharma
Updated on 15-Jun-2020 794 Views

The increment operator increases an integer value by one. Here’s an example where the value of a is incremented twice using the increment operator twiceExampleLive Demo                    var a = 33;          a = ++a;          document.write("++a = ");          result = ++a;          document.write(result);          

Read More

What is the difference between: var functionName = function() {} and function functionName() {} in Javascript

Johar Ali
Johar Ali
Updated on 15-Jun-2020 400 Views

functionDisplayOne is a function expression, however, functionDisplayTwo is a function declaration. It is defined as soon as its surrounding function is executed.Both the ways are used to declare functions in JavaScript and functionDisplayOne is an anonymous function.Here’s the function expression −functionDisplayOne(); var functionDisplayOne = function() {    console.log("Hello!"); };The following is the function declaration −functionDisplayTwo(); function functionDisplayTwo() {    console.log("Hello!"); }

Read More

How do you check if a variable is an array in JavaScript?

Rahul Sharma
Rahul Sharma
Updated on 13-Jun-2020 272 Views

To check if a variable is an array, use “instanceof. The following is the syntax −variable instanceof ArrayExampleLet’s seen an example to check if the variable “sports” is an array or not?                       var sports = [ "tennis", "football", "cricket" ];               if (sports instanceof Array) {           alert('Array!');         } else {           alert('Not an array');         }                  

Read More

What is the difference between break and continue statements in JavaScript?

Smita Kapse
Smita Kapse
Updated on 13-Jun-2020 680 Views

break statementThe break statement is used to exit a loop early, breaking out of the enclosing curly braces.  The break statement exits out of a loop.  Let’s see an example of break statement in JavaScript. The following example illustrates the use of a break statement with a while loop. Notice how the loop breaks out early once x reaches 5 and reaches to document.write (..) statement just below to the closing curly brace, Example                    var x = 1;          document.write("Entering the loop ");         ...

Read More
Showing 4821–4830 of 5,338 articles
« Prev 1 481 482 483 484 485 534 Next »
Advertisements