Sometimes in a given Python list we may be interested only in the first digit of each element in the list. In this article we will check if the first digit of all the elements in a list are same or not.With set and mapSet in Python does not allow any duplicate values in it. So we take the first digit of every element and put it in a set. If all the digits are same then the length of the set will be only 1 has no duplicates allowed.Example Live DemoAlist = [63, 652, 611, 60] # Given list print("Given ... Read More
JSON is a type of text format use to exchange data easily between various computer programs. It has a specific format which Python can validate. In this article we will consider a string and using JSON module we will validate if the string represents a valid JSON format or not.Creating JSON ObjectThe json module has method called loads. It loads a valid json string to create a Json object. In this example we load the string and check that there is no error in loading the JSON object. If there is error we consider the JSON string as invalid.Example Live Demoimport ... Read More
The Bitwise NOT (~) Operator performs NOT operation on the bits. You can try to run the following code to learn how to work with JavaScript Bitwise NOT Operator.Example document.write("Bitwise NOT Operator"); // 7 = 00000000000000000000000000000111 document.write(~7);
If one of the bits are 1, then 1 is returned when Bitwise OR (|) operator is used.ExampleYou can try to run the following code to learn how to work with JavaScript Bitwise OR Operator. document.write("Bitwise OR Operator"); // 7 = 00000000000000000000000000000111 // 1 = 00000000000000000000000000000001 document.write(7 | 1);
To get the value of the type attribute of a link in JavaScript, use the type property. The type attribute is used to tell that the document is html (text/html) or css (text/css), etc.ExampleYou can try to run the following code to get the value of the type attribute of a link.Live Demo Qries var myVal = document.getElementById("anchorid").type; document.write("Value of type attribute: "+myVal);
Yes, it is a good practice to place all the JavaScript declarations at the top. Let’s see an example −Example JavaScript String match() Method // all the variables declared at top var str, re, found; str = "For more information, see Chapter 3.4.5.1"; re = /(chapter \d+(\.\d)*)/i; found = str.match( re ); document.write(found ); The following are the valid reasons −Gives a single place to check for all the variables.Helps in avoiding global variablesRe-declarations are avoided.The code is easy to read for others as well.
If a semicolon is misplaced in JavaScript, then it may lead to misleading results. Let’s see an example, wherein the if statement condition is false, but due to misplaced semi-colon, the value gets printed.Example var val1 = 10; if (val1 == 15) { document.write("Prints due to misplaced semi-colon: "+val1); } var val2 = 10; if (val2 == 15) { // this won't get printed document.write(val2); }
To show all the options from a dropdown list, use the options property. The property allows you to get all the options with length property.ExampleYou can try to run the following code to get all the options from a drop-down list.Live Demo One Two Three Click the button to get all the options function display() { var a, i, options; a = document.getElementById("selectNow"); options = ""; for (i = 0; i < a.length; i++) { options = options + " " + a.options[i].text; } document.write("DropDown Options: "+options); }
You can try to run the following to learn how to convert String to Boolean in JavaScript.ExampleLive Demo Convert String to Boolean var myString = "Amit"; document.write("Boolean : " + Boolean(myString));
Use the JavaScript fixed() method to display in fixed-pitch font as if it were in a tag.ExampleYou can try to run the following code to display a string in the fixed-pitch font. JavaScript String fixed() Method var str = new String("Hello world"); alert(str.fixed());
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP