
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 6710 Articles for Javascript

192 Views
Let’s say the following is our array with similar values −const listOfStudentName = ['John', 'Mike', 'John', 'Bob', 'Mike', 'Sam', 'Bob', 'John'];To remove similar values from array, use the concept of set(). Following is the code −Exampleconst listOfStudentName = ['John', 'Mike', 'John', 'Bob', 'Mike', 'Sam', 'Bob', 'John']; console.log("The value="+listOfStudentName); const doesNotContainSameElementTwice = [...new Set(listOfStudentName)]; console.log("The Array="); console.log(doesNotContainSameElementTwice)To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo42.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo42.js The value=John, Mike, John, Bob, Mike, Sam, Bob, John The Array= [ 'John', 'Mike', 'Bob', 'Sam' ]Read More

21K+ Views
Use the condition with “” and NULL to check if value is empty. Throw a message whenever ua ser does not fill the text box value.Example Live Demo Document USERNAME: function checkingUserName() { var username = document.forms["register"]["username"].value; if (username == null || username == "") { alert("Please enter> the username. Can’t be blank or empty !!!"); return false; } } To run the above program, save ... Read More

286 Views
Yes, you can use Object.fromEntries() along with substr(). Under substr(), mention the index from where to begin the substring and the length.Exampleconst originalString = { "John 21 2010" :1010, "John 24 2012" :1011, "John 22 2014" :1012, "John 22 2016" :1013, } const result = Object.fromEntries(Object.entries(originalString). map(([k, objectValue])=> [k.substr(0, k.length-5), objectValue])); console.log(result)To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo41.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo41.js { 'John 21': 1010, 'John 24': 1011, 'John 22': 1013 }

5K+ Views
Get data from tag using find() and store that data into array using push(). Let’s say the following is our table − Name Age John23 David26 Let’s fetch the data from and store in an array. Following is the complete code −Example Live Demo Document .notShown { display: none; } Name Age John23 David26 var convertedIntoArray = []; $("table#details tr").each(function() { var rowDataArray = ... Read More

2K+ Views
Take an array to store the frequency of each character. If similar character is found, then increment by one otherwise put 1 into that array.Let’s say the following is our string −var sentence = "My name is John Smith";Following is the JavaScript code to count occurrences −Examplevar sentence = "My name is John Smith"; sentence=sentence.toLowerCase(); var noOfCountsOfEachCharacter = {}; var getCharacter, counter, actualLength, noOfCount; for (counter = 0, actualLength = sentence.length; counter < actualLength; ++counter) { getCharacter = sentence.charAt(counter); noOfCount = noOfCountsOfEachCharacter[getCharacter]; noOfCountsOfEachCharacter[getCharacter] = noOfCount ? noOfCount + 1: 1; } for (getCharacter in noOfCountsOfEachCharacter) { ... Read More

694 Views
Let’s say we have the following string with special character sequence −var fullName=" John Smith ";To separate the above string into substring, use regex and then split(). The syntax is as follows −var anyVariableName=(/\s*\s*/g); var anyVariableName=yourVariableName.trim().split(yourVariableName);Following is the complete JavaScript code −Examplevar fullName=" John Smith "; console.log("The Value="+fullName); var regularExpression=(/\s*\s*/g); var seprateName=fullName.trim().split(regularExpression); console.log(seprateName);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo39.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo39.js The Value= John Smith [ 'John', 'Smith' ]Read More

599 Views
To round the decimal number to the nearest tenth, use toFixed(1) in JavaScript. The syntax is as follows −var anyVaribleName=yourVariableName.toFixed(1)Let’s say the following is our decimal number −var decimalValue =200.432144444555; console.log("Actual value="+decimalValue)Let’s now round the decimal number. Following is the code −Examplevar decimalValue =200.432144444555; console.log("Actual value="+decimalValue) var modifiedValue=decimalValue.toFixed(1) console.log("Modified value="+ modifiedValue);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo38.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo38.js Actual value=200.432144444555 Modified value=200.4Read More

302 Views
The easiest way to detect keypresses in JavaScript, use the onKeyPress event handler −document.onkeypressThe key press is matched with the keyCode property, which returns the Unicode character code of the key that triggered the onkeypress event.Example Live Demo Document document.onkeypress = function (eventKeyName) { eventKeyName = eventKeyName || window.event; if(eventKeyName.keyCode==13){ console.log('You have pressed enter key'); } else { alert(String.fromCharCode(eventKeyName.keyCode)) } }; To run the above program, save the file name ... Read More

674 Views
To find out which characters key is pressed, use the window.event along with keyCode. Following is the code −Example Live Demo Document function keyPressName(myEventKeyName){ var pressedKey; if(window.event){ pressedKey = myEventKeyName.keyCode; } else if(myEventKeyName.which) { pressedKey = myEventKeyName.which; } alert(String.fromCharCode(pressedKey)); } To run the above program, save the file name “anyName.html(index.html)” and right click on the file. Select the option “Open with Live Server” ... Read More

2K+ Views
To avoid unexpected string concatenation while concatenating strings, multiple strings, and numbers, use backticks.We have the following −const concatValue = 'John, David, Mike'; var friendNames= `${concatValue}`;The above value is concatenated with a string and number −var studentNameWithFriends=` ${concatValue}| 'Carol' | 24 ` ;Following is the complete JavaScript code for concatenation −Exampleconst concatValue = 'John, David, Mike'; var friendNames= `${concatValue}`; var studentNameWithFriends=` ${concatValue}| 'Carol' | 24 ` ; console.log(friendNames); console.log(studentNameWithFriends);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo37.jsOutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo37.js John, David, Mike John, ... Read More