Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Javascript Articles
Page 483 of 534
How to create arrays in JavaScript?
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 MoreHow to convert a Boolean to Integer in JavaScript?
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 MoreHow to get the first index of an occurrence of the specified value in a string in JavaScript?
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 MoreHow to convert a boolean value to string value in JavaScript?
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 MoreHow to create a strikethrough text using JavaScript?
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 MoreWhy avoid increment (“++”) and decrement (“--”) operators in JavaScript?
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 MoreWhat is increment (++) operator in JavaScript?
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 MoreWhat is the difference between: var functionName = function() {} and function functionName() {} in Javascript
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 MoreHow do you check if a variable is an array in JavaScript?
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 MoreWhat is the difference between break and continue statements in JavaScript?
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