Found 6710 Articles for Javascript

Split First name and Last name using JavaScript?

AmitDiwan
Updated on 12-Sep-2020 08:10:26

2K+ Views

Let’s say the following is our string with name −var studentFullName="John Smith";Use split() to split the first name and last name. Following is the code −Examplevar studentFullName="John Smith"; var details=[] var details=studentFullName.split(' '); console.log("StudentFirstName="+details[0]) console.log("StudentLastName="+details[1]);To run the above program, you need to use the following command −node fileName.js.OutputHere, my file name is demo163.js. This will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo163.js StudentFirstName=John StudentLastName=Smith

Difference between Primitive and non-primitive datatypes in JavaScript?

AmitDiwan
Updated on 12-Sep-2020 08:06:44

1K+ Views

The primitive data types are number, string, boolean, float etc. The non-primitive data types (Reference Type) are Array, Object etc.Examplevar number=10; var stringValue="John"; var booleanValue=true; var obj={}; var newArray=new Array(); console.log("The data type is="+typeof number); console.log("The data type is="+typeof stringValue); console.log("The data type is="+typeof booleanValue); console.log("The data type is="+typeof obj); console.log("The data type is="+typeof newArray);To run the above program, you need to use the following command −node fileName.js.OutputHere, my file name is demo162.js. This will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo162.js The data type is=number The data type is=string The data type is=boolean The data type is=object The ... Read More

Enter key press event in JavaScript?

AmitDiwan
Updated on 15-Sep-2023 02:16:05

26K+ Views

For ENTER key press event, you can call a function on −onkeypress=”yourFunctionName”Use the ENTER’s keycode 13.Example Live Demo Document    function enterKeyPressed(event) {       if (event.keyCode == 13) {          console.log("Enter key is pressed");          return true;       } else {          return false;       }    } 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 −On pressing ENTER key, the following output is visible on console −

Extract hostname from URL string in JavaScript?

AmitDiwan
Updated on 12-Sep-2020 07:54:38

674 Views

To extract hostname from URL string, use split() function. Following is the code −Examplefunction gettingTheHostNameFromURL(websiteURL) {    var getTheHostName;    if (websiteURL.indexOf("//") > -1) {       getTheHostName = websiteURL.split('/')[2];    } else {       getTheHostName = websiteURL.split('/')[0];    }    getTheHostName = getTheHostName.split(':')[0];    getTheHostName = getTheHostName.split('?')[0];    return getTheHostName; } var websiteURL="https://www.tutorialspoint.com/java/index.htm";var websiteURL1="https://www.tutorix.com/about_us.htm"; var websiteURL2="https://www.tutorialspoint.com/execute_python_online.php"; console.log(gettingTheHostNameFromURL(websiteURL)) console.log(gettingTheHostNameFromURL(websiteURL1)) console.log(gettingTheHostNameFromURL(websiteURL2))To run the above program, you need to use the following command −node fileName.js.OutputHere, my file name is demo161.js. This will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo161.js www.tutorialspoint.com www.tutorix.com www.tutorialspoint.comRead More

Make HTML text input field grow as I type in JavaScript?

AmitDiwan
Updated on 12-Sep-2020 07:51:37

466 Views

For this, use . Since you want to type in it, do not forget to set as −contenteditable="true"Example Live Demo Document    span{       border: solid 2px skyblue;    }    div{       max-width: 500px;    } John Smith 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 −

How can I find the index of a 2d array of objects in JavaScript?

AmitDiwan
Updated on 12-Sep-2020 07:48:55

1K+ Views

To find the index of a two-dimensional array of objects, use two for loops, one for row and another for column. Following is the code −Examplefunction matrixIndexed(details, name) {    var r;    var c;    for (r = 0; r < details.length; ++r) {       const nsDetails = details[r];       for (c = 0; c < nsDetails.length; ++c) {          const tempObject = nsDetails[c];          if (tempObject.studentName === name) {             return { r, c};          }       } ... Read More

How to get sequence number in loops with JavaScript?

AmitDiwan
Updated on 12-Sep-2020 07:46:59

975 Views

To get sequence number in loops, use the forEach() loop. Following is the code −Examplelet studentDetails = [    {       id: 101, details: [{name: 'John'}, {name: 'David'},{name: 'Bob'}]},       {id:102, details: [{name:'Carol'},{name:'David'},       {name:'Mike'}]    } ]; var counter = 1; studentDetails.forEach(function(k){    k.details.forEach(function(f) {          console.log(counter++);       }    ); });To run the above program, you need to use the following command −node fileName.js.OutputHere, my file name is demo159.js. This will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo159.js 1 2 3 4 5 6

The best way to remove duplicates from an array of objects in JavaScript?

AmitDiwan
Updated on 12-Sep-2020 07:44:33

219 Views

Let’s say the following is our array of objects with duplicates −var studentDetails=[    {studentId:101},    {studentId:104},    {studentId:106},    {studentId:104},    {studentId:110},    {studentId:106}, ]Use the concept of set to remove duplicates as in the below code −Examplevar studentDetails=[    {studentId:101},    {studentId:104},    {studentId:106},    {studentId:104},    {studentId:110},    {studentId:106}, ] const distinctValues = new Set const withoutDuplicate = [] for (const tempObj of studentDetails) {    if (!distinctValues.has(tempObj.studentId)) {       distinctValues.add(tempObj.studentId)       withoutDuplicate.push(tempObj)    } } console.log(withoutDuplicate);To run the above program, you need to use the following command −node fileName.js.OutputHere, my file name ... Read More

Appending a key value pair to an array of dictionary based on a condition in JavaScript?

AmitDiwan
Updated on 12-Sep-2020 07:42:51

1K+ Views

For this, use Object.assign(). Following is the code −Exampleconst details = {john:{'studentName':'John'}, david:{'studentName':'David'}, mike:{'studen tName':'Mike'}, bob:{'studentName':'Bob'}, carol:{'studentName':'Carol'}}, join_values = ['David', 'Carol'],    output = Object.assign(       {},       ...Object       .keys(details)       .map(key =>       ({[key]: {          ...details[key],          lastName: join_values.includes(details[key].studentName) ?          'Miller' : 'Smith'    }}))) console.log(output)To run the above program, you need to use the following command −node fileName.js.OutputHere, my file name is demo157.js. This will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo157.js {    john: ... Read More

Create increment decrement plus minus buttons programmatically for HTML input type number in JavaScript

AmitDiwan
Updated on 12-Sep-2020 07:39:10

16K+ Views

We will be creating two buttons, one for Increment and another Decrement −On clicking Increment (+), user will be able to increment the number in input type numberOn clicking Decrement (-), user will be able to decrement the number in input type numberExample Live Demo Document + -    function increment() {       document.getElementById('demoInput').stepUp();    }    function decrement() {       document.getElementById('demoInput').stepDown();    } To run the above program, save the file name “anyName.html(index.html)” and right click on the file. Select the option ... Read More

Advertisements