To modify string, you can use toLowerCase() as well as toUpperCase(). Let’s say the following is our string −var sentence = "tHIS iS tHE JavaScript pROGRAM";To modify and display in proper case, the code is as follows −Examplevar sentence = "tHIS iS tHE JavaScript pROGRAM"; function modifyStringWithMultipleMethods(sentence) { return sentence.charAt(0).toUpperCase() + sentence.slice(1).toLowerCase(); } console.log(modifyStringWithMultipleMethods(sentence));To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo51.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo51.js This is the JavaScript programRead More
To remove text, use the concept of remove(). Use filter to get the content not inside element tag.Let’s say the following is our HTML −Demo Program This is also Demo ProgramAnd we have to remove “This is also Demo Program” since it is not under element tag. For that, the compete code is as follows using filter() and remove() −Example Live Demo Document Demo Program This is also Demo Program $('body').contents().filter(function(){ return this.nodeType != 1; }).remove(); To run the above program, save the file ... Read More
Let’s say the following is our nested array −const arrayObject = [ [ { Name: "John" }, { countryName: "US" } ], [ { subjectName: "JavaScript" }, { teacherName: "Mike" } ] ];To transform nested array into normal array, use the concept of flat() as in the below code −Exampleconst arrayObject = [ [ { Name: "John" }, { countryName: "US" } ], [ { subjectName: "JavaScript" }, { teacherName: "Mike" } ] ]; const output = arrayObject.flat(); console.log(output);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo50.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo50.js [ { Name: 'John' }, { countryName: 'US' }, { subjectName: 'JavaScript' }, { teacherName: 'Mike' } ]
To get the value of H1 to JavaScript variable, you can use −document.getElementById().innerHTML.Let’s say the following is our H1 heading − This is the demo program of JavaScript ........Now, let’s get the H1 value using the below code −Example Live Demo Document This is the demo program of JavaScript ........ var data=document.getElementById('demo').innerHTML; console.log("The data is="+data); 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 ... Read More
Let’s say we have records with BOTID and Name of assigned users −let objectArray = [ { BOTID: "56", Name: "John" }, { BOTID: "57", Name: "David" }, { BOTID: "58", Name: "Sam"}, { BOTID: "59", Name: "Mike" }, { BOTID: "60", Name: "Bob" } ];We know the array starts from index 0. If you want to access the first element from the above array, use the below syntax −var anyVariableName=yourArrayObjectName[index].yourFieldName;Examplelet objectArray = [ { BOTID: "56", Name: "John" }, { BOTID: "57", Name: "David" }, { BOTID: "58", Name: "Sam"}, ... Read More
To add a newline in Unordered List, use the document.querySelector().append(). Following is the code −Example Live Demo Document h1{ font-size: 2.50rem; } h2, label{ font-size: 1.50rem; } Adding Name Demo Enter The Name: Save List Of Name const buttonName = document.querySelector('.btnName') const addName = e => { let nameTxt = document.querySelector('.txtName'), name = nameTxt.value.trim() if (name) { let tagLi = ... Read More
At first, set the keys −const name = ['Name1', 'Name2'];Now, map keys to the same value using for loop as in the below code −Exampleconst name = ['Name1', 'Name2']; const keyValueObject = {}; for (const k of name){ keyValueObject[k] = 'John'; } console.log(keyValueObject);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo48.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo48.js { Name1: 'John', Name2: 'John' }
To remove and add new HTML tags, use the concept of hide() and show().Let’s say the following are our buttons −Click Me To hide above content Click Me To show above content To remove and add tags on button clicks, use hie() and show() −$(document).ready(function(){ $("#hide").click(function(){ $("h1").hide(); }); $("#show").click(function(){ $("h1").show(); }); });Example Live Demo Document Test JavaScript Click Me To hide above content Click Me To show above content $(document).ready(function(){ $("#hide").click(function(){ ... Read More
Let’s say the following are our values −let subjectNames = ['JavaScript', 'Angular', 'AngularJS', 'Java'];To count the non-empty and non-null values, use the forEach(). The syntax is as follows −yourArrayName.forEach(anyVariableName =>{ yourStatement1 . . . N } } )Now, use the if statement and check −var count=0 subjectNames.forEach(subject =>{ if(subject!=' ' || subject!=null){ count+=1; } } )Examplelet subjectNames = ['JavaScript', 'Angular', 'AngularJS', 'Java']; var count=0 subjectNames.forEach(subject =>{ if(subject!=' ' || subject!=null){ count+=1; } } ) console.log("Number of subject=="+count);To ... Read More
Let’s say, we have the following objects in JavaScript −ObjectValue =[ { "id": "101", "details": { Name:"John", subjectName:"JavaScript" }}, { "id": "102", "details": { Name:"David", subjectName:"MongoDB" }}, { "id": "103" } ]To remove an object using filter(), the code is as follows −ExampleObjectValue =[ { "id": "101", "details": { Name:"John", subjectName:"JavaScript" }}, { "id": "102", "details": { Name:"David", subjectName:"MongoDB" }}, { "id": "103" } ] output=ObjectValue.filter(obj=>obj.details) console.log(output)To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo46.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo46.js ... Read More