Create Empty Array of a Given Size in JavaScript

AmitDiwan
Updated on 03-Sep-2020 06:45:54

2K+ Views

To create an empty array of given size, use the new operator −var numberArray = new Array(10);After that, let’s set some values in the array. Following is the code −Examplevar numberArray = new Array(10); console.log("The length="+numberArray.length) numberArray=[10,20,30,40,50,60]; console.log("The array value="); for(var i=0;i node demo52.js The length=10 The array value= 10 20 30 40 50 60

Run Multiple Functions with OnClick in JavaScript

AmitDiwan
Updated on 03-Sep-2020 06:44:39

574 Views

Let’s first set a button − Call Above, we have set a function under “onclick” to call two other functions −function callTwoOtherFunctions(){    fun1();    fun2(); }In this way, work around the fun1() and fun2() as in the complete code below −Example Live Demo Document Call    function callTwoOtherFunctions(){       fun1();       fun2();    }    function fun1(){       console.log("Function1()")    }    function fun2(){       console.log("Function2()")    } To run the above program, save the file name “anyName.html(index.html)” ... Read More

Check Valid Date Format in JavaScript

AmitDiwan
Updated on 03-Sep-2020 06:41:38

682 Views

To check for valid date format, match the date with −const dateFormat = /^\d{4}\-\d{2}\-\d{2}$/;Example Live Demo Document    .check-valid-date {       border: 1px solid red;    }    const dateFormat = /^\d{4}\-\d{2}\-\d{2}$/;    document.getElementById("check-valid-date").addEventListener("change",    checkingForValidDate);    function checkingForValidDate() {       console.log(this.value, dateFormat.test(this.value));       this.classList.toggle('check-valid-date',       dateFormat.test(this.value));    } 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 ... Read More

Better Ways to Modify String with Multiple Methods Using JavaScript

AmitDiwan
Updated on 03-Sep-2020 06:37:11

390 Views

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

Remove Any Text Not Inside Element Tag on a Web Page with JavaScript

AmitDiwan
Updated on 03-Sep-2020 06:32:47

2K+ Views

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

Transform Nested Array into Normal Array with JavaScript

AmitDiwan
Updated on 03-Sep-2020 06:27:27

278 Views

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' } ]

Get HTML H1 Value to JavaScript Variable

AmitDiwan
Updated on 03-Sep-2020 06:25:33

4K+ Views

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

Get First Bot ID from JavaScript Array

AmitDiwan
Updated on 03-Sep-2020 06:21:00

107 Views

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

Add Newline in Unordered List (UL) from JavaScript

AmitDiwan
Updated on 03-Sep-2020 06:19:46

352 Views

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

Instantiate Dictionary in JavaScript with Same Value for All Keys

AmitDiwan
Updated on 03-Sep-2020 06:14:35

285 Views

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' }

Advertisements