AmitDiwan has Published 10744 Articles

Make first letter of a string uppercase in JavaScript?

AmitDiwan

AmitDiwan

Updated on 12-Sep-2020 08:17:51

431 Views

To make first letter of a string uppercase, use toUpperCase() in JavaScript. With that, we will use charAt(0) since we need to only capitalize the 1st letter.Examplefunction replaceWithTheCapitalLetter(values){    return values.charAt(0).toUpperCase() + values.slice(1); } var word="javascript" console.log(replaceWithTheCapitalLetter(word));To run the above program, you need to use the following command −node fileName.js.Here, ... Read More

How to ignore using variable name as a literal while using push() in JavaScript?

AmitDiwan

AmitDiwan

Updated on 12-Sep-2020 08:16:41

237 Views

To avoid using variable name as a literal, use square brackets. Following is the code −Examplevar name = "David" var putTheAllData = [] putTheAllData.push( { name: "The name is name will remain same" } ) putTheAllData.push( { [name]: "The name is David will be changed [name]"} ) console.log(putTheAllData);To run the ... Read More

Add object to array in JavaScript if name does not already exist?

AmitDiwan

AmitDiwan

Updated on 12-Sep-2020 08:12:53

942 Views

For this, use push() along with forEach(). Following is the code −Examplevar details = [{name:"John"}, {name:"David"}] var addObject = ["Mike", "Sam"]; addObject.forEach( obj1 => {    if(!details.find( obj2 => obj2===obj1 ))       details.push({name:obj1}) }) console.log(details);To run the above program, you need to use the following command −node fileName.js.Here, ... Read More

Replace commas with JavaScript Regex?

AmitDiwan

AmitDiwan

Updated on 12-Sep-2020 08:11:28

674 Views

Let’s say the following are our strings with commas −"My Favorite subject is, " "My Favorite subject is, and teacher name is Adam Smith" "My Favorite subject is, and got the marks 89"To replace commas, use replace and in that, use Regular Expression. Following is the code −Exampleconst makingRegularExpression = ... Read More

Split First name and Last name using JavaScript?

AmitDiwan

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 ... Read More

Difference between Primitive and non-primitive datatypes in JavaScript?

AmitDiwan

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); ... Read More

Extract hostname from URL string in JavaScript?

AmitDiwan

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 = ... Read More

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

AmitDiwan

AmitDiwan

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

464 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;    } ... Read More

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

AmitDiwan

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 ... Read More

How to get sequence number in loops with JavaScript?

AmitDiwan

AmitDiwan

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

974 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; ... Read More

Advertisements