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 359 of 534
Split a URL in JavaScript after every forward slash?
To split a URL, use the split() method. Use toString() before that. Let us see an exampleExamplevar newURL="http://www.example.com/index.html/homePage/aboutus/"; console.log(newURL); var splitURL=newURL.toString().split("/"); console.log(splitURL);Above, we have set forward slash in the split() function, since we need to split the URL after every such slash.To run the above program, you need to use the following command −node fileName.js.OutputHere, my file name is demo150.js. This will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo150.js http://www.example.com/index.html/homePage/aboutus/[ 'http:', '', 'www.example.com', 'index.html', 'homePage', 'aboutus', '' ]
Read MoreCan someone explain to me what the plus sign is before the variables in JavaScript?
The plus(+) sign before the variables defines that the variable you are going to use is a number variable.In the below code, there is brief description about plus sign. Following is the code −Examplevar firstValue="1000"; console.log("The data type of firstValue ="+typeof firstValues); var secondValue=1000; console.log("The data type of secondValue ="+typeof secondValue); console.log("The data type of firstValue when + sign is used ="+typeof +firstValue); var output=+firstValue + secondValue; console.log("Addition is="+output);To run the above program, you need to use the following command −node fileName.js.OutputHere, my file name is demo149.js. This will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo149.js The data type ...
Read MoreDisplay a message on console while focusing on input type in JavaScript?
For this, you can use the concept of focus(). Following is the code −Example Live Demo Document Submit const submitButton = document.getElementById('submitBtn'); submitButton.addEventListener('click', () => { console.log('Submitting information to the server.....'); }) const txtInput = document.getElementById('txtInput'); txtInput.addEventListener('blur', () => { console.log('Enter the new value into the text box......'); txtInput.focus(); }); txtInput.focus(); To run the above program, save the file name “anyName.html(index.html)” and right click on the file. Select the option “Open ...
Read MoreFilter null from an array in JavaScript?
To filter null from an array and display only non-null values instead, use filter(). Following is the code −Examplevar names=[null, "John", null, "David", "", "Mike", null, undefined, "Bob", "Adam", null, null]; console.log("Before filter null="); console.log(names); var filterNull=[]; filterNullValues=names.filter(obj=>obj); console.log("After filtering the null values="); console.log(filterNullValues);To run the above program, you need to use the following command −node fileName.js.OutputHere, my file name is demo148.js. This will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo148.js Before filter null=[ null, 'John', null, 'David', '', 'Mike', null, undefined, 'Bob', 'Adam', null, null ] After filtering the null values= [ ...
Read MoreRetrieving object's entries in order with JavaScript?
Let’s say the following is our object −const subjectDetails ={ 102:"Java", 105:"JavaScript", 104:"MongoDB", 101:"MySQL" };Use sort() method to retrieve object’s entry in order −const orderSubjects = Object.fromEntries(Object.keys(subjectDetails).sort().map((k) => { return [k, subjectDetails[k]]; }));Exampleconst subjectDetails ={ 102:"Java", 105:"JavaScript", 104:"MongoDB", 101:"MySQL" }; const orderSubjects = Object.fromEntries(Object.keys(subjectDetails).sort().map((k) => { return [k, subjectDetails[k]]; })); console.log(orderSubjects);To run the above program, you need to use the following command −node fileName.js.OutputHere, my file name is demo146.js. This will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo146.js{ '101': 'MySQL', '102': 'Java', '104': 'MongoDB', '105': ...
Read MorePrint JSON nested object in JavaScript?
To print JSON nested object in JavaScript, use for loop along with JSON.parse(). Following is the code −Examplevar details = [ { "studentId": 101, "studentName": "John", "countryName": "US", "subjectDetails": "{\"0\":\"JavaScript\",\"1\":\"David\"}" }, { "studentId": 102, "studentName": "Bob", "countryName": "UK", "subjectDetails": "{\"0\":\"Java\",\"1\":\"Carol\"}" }, { "studentId": 103, "studentName": "Mike", "countryName": "AUS", "subjectDetails": "{\"0\":\"MongoDB\",\"1\":\"Adam\"}" } ] for (const detailsObject of details) { const subjectDetailsObject = JSON.parse(detailsObject.subjectDetails); console.log(subjectDetailsObject[0]); }To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo145.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo145.js JavaScript Java MongoDB
Read MoreGetting HTML form values and display on console in JavaScript?
To get HTML form values, use the value property. Let’s say the following is our input type −We need to display the above value “My name is John Smith” on console.Example Live Demo Document var originalvalue = document.getElementById("getValues").value; console.log("The value in the text box is ="+originalvalue); 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” in VS Code editor.OutputThis will produce the following output −
Read MoreFind the number of times a value of an object property occurs in an array with JavaScript?
For this, use the concept of reduce(). Following is the code −Exampleconst subjectDetails = [ { subjectId: '101', subjectName: 'JavaScript' }, { subjectId: '102', subjectName: 'Java' }, { subjectId: '103', subjectName: 'JavaScript' } ]; console.log([...subjectDetails.reduce((obj1, obj2) => { if (obj1.has(obj2.subjectName)){ obj1.get(obj2.subjectName).frequency++; } else { obj1.set(obj2.subjectName, { subjectName: obj2.subjectName, frequency: 1 }); } return obj1; }, new Map()).values()]);To run the above program, you need ...
Read MoreHow to add and remove names on button click with JavaScript?
To create, use add() method, whereas to delete created and appended element, you can use remove(). Following is the code −Example Live Demo Document Add A New Name AddName var givenName = document.querySelector('#name') var btnClass = document.querySelector('#addNameButton') var listOfName = document.querySelector('#listOfName') btnClass.addEventListener('click', () => { var actualName = givenName.value if (actualName.length != 0) { var createAnHTMLList = `${actualName}Remove Name` listOfName.innerHTML += ...
Read MoreRemove Seconds/ Milliseconds from Date and convert to ISO String?
Let’s get the current date first −var currentDate = new Date(); console.log("The current date is as follows="+currentDate);Now, let us remove seconds/ milliseconds by setting them to 0 using setSeconds() −currentDate.setSeconds(0, 0);Convert to ISO String using toISOString() −currentDate.toISOString()Let us now see the complete code with output −Examplevar currentDate = new Date(); console.log("The current date is as follows="+currentDate); currentDate.setSeconds(0, 0); console.log("After removing seconds from date, the new date is as follows="); console.log(currentDate.toISOString());To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo143.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo143.js The current date ...
Read More