In order to convert string into camel case, you need to lowercase the first letter of the word and the first letter of the remaining words must be in capital.Following is the code to convert any string into camel case −Examplefunction convertStringToCamelCase(sentence) { return sentence.replace(/(?:^\w|[A-Z]|\b\w|\s+)/g, function(camelCaseMatch, i) { if (+camelCaseMatch === 0) return ""; return i === 0 ? camelCaseMatch.toLowerCase() : camelCaseMatch.toUpperCase(); }); } console.log(convertStringToCamelCase("Add two variables"));To run the above program, you need to use the following command −node fileName.js.Here, my file name is ... Read More
To display only the current year, use getFullYear(). Following is the code −Example Live Demo Document The Current Year= document.getElementById("theCurrentYear").innerHTML = new Date().getFullYear(); 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 −
For this, you can use replace() along with parse(). Following is the code −Examplevar studentDetails = `"{""name"": ""John"", ""subjectName"": ""Introduction To JavaScript""}"`; console.log("The actual object="); console.log(studentDetails); var removeFirstAndLast = studentDetails.substring(1, studentDetails.length-1) var removeDoubleQuotes = removeFirstAndLast.replace(/""/gi, `"`) console.log(removeDoubleQuotes) var output = JSON.parse(removeDoubleQuotes); console.log(output);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo103.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo103.js The actual object= "{""name"": ""John"", ""subjectName"": ""Introduction To JavaScript""}" {"name": "John", "subjectName": "Introduction To JavaScript"} { name: 'John', subjectName: 'Introduction To JavaScript' }Read More
The Object.assign() method is used to copy one or more source objects to a target object. It invokes getters and setters since it uses both 'get' on the source and 'Set' on the target.The syntax is as follows −Object.assign(target, ...source objects);Following is the code to copy object −Examplevar object = {first: second => second + 1000} var object2= Object.assign({}, object); console.log("The result="); console.log(object2.first(1000));To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo102.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo102.js The result= 2000Read More
Let’s say the following are our elements −My Name is John My Name is David My Name is Bob My Name is Mike My Name is Carol ENDWe need to remove theelement and its content. Theelements are in between the START and END.To remove elements between two elements, use the concept of remove(). Following is the code −Example Live Demo Document START My Name is John My Name is David My Name is Bob My Name is Mike My Name is Carol END const startingPoint = document.querySelector("nav"); const endingPoint = document.querySelector("footer"); ... Read More
For this, use document.querySelectorAll(). With that, also use the getElementsByClassName(). Following is the code −Example Live Demo Document My Name is John My h6 value must be here... My h6 value must be here... My h6 value must be here... const allSpan = document.querySelectorAll('.my-main-div-class span'), repaceAboveText = document.getElementsByClassName('uniqueId')[0].textContent; for (var newElement of allSpan){ newElement.textContent=repaceAboveText } 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 −
Let’s say the following are our array of objects −var studentDetails = [ { firstName: "John", listOfSubject: ['MySQL', 'MongoDB']}, {firstName: "David", listOfSubject: ['Java', 'C'] }]We need to add the following in the already created array of objects −{firstName: "Bob", listOfSubject: ['JavaScript']};Examplevar studentDetails = [ { firstName: "John", listOfSubject: ['MySQL', 'MongoDB']}, {firstName: "David", listOfSubject: ['Java', 'C']}]; updateThisObject = {firstName: "Bob", listOfSubject: ['JavaScript']}; function forLoopExample(studentObjects, updateObj){ for(var index = 0;index < studentObjects.length; index++) { if(updateObj.listOfSubject.join("") === studentObjects[index].listOfSubject.join("")) { studentObjects[index] = updateObj; ... Read More
Let’s say the following are our objects −var first = {key1: 100, key2: 40, key3: 70} var second = {key2: 80, key3: 70, key4: 1000}You can use the concept of hasOwnProperty() to add properties from one object to another. Following is the code −Examplevar first = {key1: 100, key2: 40, key3: 70} var second = {key2: 80, key3: 70, key4: 1000} function addPropertiesWithoutOverwritting(first, second) { for (var key2 in second) { if (second.hasOwnProperty(key2) && !first.hasOwnProperty(key2)) { first[key2] = second[key2]; } } return first; } console.log(addPropertiesWithoutOverwritting(first, second))To run ... Read More
For this, use filter() along with map(). Let’s say the following is our array −const studentDetails = [ {Name: "John"}, {Name: "David"}, {Name: "Bob"}, {Name: "Mike"} ]We will assign a new value to the name “Bob”. Following is the code −Exampleconst studentDetails = [ {Name: "John"}, {Name: "David"}, {Name: "Bob"}, {Name: "Mike"} ] var changeName = "Bob"; studentDetails.filter((obj) => obj.Name === changeName).map((obj) => obj.Name = "Carol"); console.log(studentDetails);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo98.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> ... Read More
Use the concept of freeze() from JavaScript to disallow adding new properties to an object, altering of object properties, etc.Following is the code wherein we are changing the value but the previous value still remains since we cannot alter properties using freeze() −Exampleconst canNotChangeTheFieldValueAfterFreeze = {value1 : 10, value2: 20 }; Object.freeze(canNotChangeTheFieldValueAfterFreeze); canNotChangeTheFieldValueAfterFreeze.value = 100; console.log("After changing the field value1 from 10 to 100 ="+canNotChangeTheFieldValueAfterFreeze.value1);To run the above program, you need to use the following command −node fileName.js.OutputHere, my file name is demo97.js. This will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo97.js After changing the field value1 from 10 ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP