ES6/ECMA6 template literals not working in JavaScript?

AmitDiwan
Published on 26-Oct-2020 15:58:35
In order to use template literals, you need to use backticks(` `) not single quotes(‘ ‘).ExampleFollowing is the code −var firstName="John"; var lastName="Smith"; console.log(`First Name is= ${firstName}\nLast Name is= ${lastName} `);To run the above program, you need to use the below command −node fileName.js.Here, my file name is demo320.js.OutputThis will ... Read More

JavaScript regex - How to replace special characters?

AmitDiwan
Published on 26-Oct-2020 15:57:00
To replace special characters, use replace() in JavaScript.The syntax is as follows −anyVariableName.replace(/(^\anySymbol)|, /g, '');ExampleFollowing is the code −var values = '&677, 431, 444, 98777'; var result=values.replace(/(^\&)|, /g, ''); console.log(result);To run the above program, you need to use the below command −node fileName.js.Here, my file name is demo319.js.OutputThis will produce ... Read More

Get the number of true/false values in an array using JavaScript?

AmitDiwan
Published on 26-Oct-2020 15:54:49
To get the number of true/ false values in an array, the code is as follows −var obj = [    {       isMarried: true    },    {       isMarried: false    },    {       isMarried: true    },    {   ... Read More

Is it possible to change the value of the function parameter in JavaScript?

AmitDiwan
Published on 26-Oct-2020 15:52:04
Yes, you can increment and assign to some other variable and can return the incremented value.ExampleFollowing is the code −function changeTheValueOfFunctionParameter(value) {    var incrementValue = value + 1;    return incrementValue; } var value = 190; console.log('The value is:', value); value = changeTheValueOfFunctionParameter(value); console.log('After changing the value:', value);To run ... Read More

Set object property in an array true/false, whether the id matches with any id from another array of objects in JavaScript?

AmitDiwan
Published on 26-Oct-2020 15:49:54
To set object property in an array true/false, whether the id matches with any id from another array of objects, you can use reduce() along with map().ExampleFollowing is the code −let firstDetails=[{"studentId":101, "studentName":"John"}, {"studentI d":102, "studentName":"David"}, {"studentId":103, "studentName":"B ob"}] let secondDetails=[{"studentId":101, "studentName":"Robert"}, {"stude ntId":109, "studentName":"Mike"}, {"studentId":103, "studentName": "Adam"}] const obj ... Read More

Add class name in different li by pure in JavaScript?

AmitDiwan
Published on 26-Oct-2020 15:46:53
To add class, use forEach() along with classList.add().ExampleFollowing is the code −<!DOCTYPE html> <html lang="en"> <head>    <meta charset="UTF-8">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>Document</title> </head> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <style>    .demo1 {       color: yellow;    }    .demo2 {     ... Read More

Understanding the find() method to search for a specific record in JavaScript?

AmitDiwan
Published on 26-Oct-2020 15:44:49
To fetch a specific record, use find() with some condition.ExampleFollowing is the code −var obj=[    {       studentId:101,       studentName:"John"    },    {       studentId:102,       studentName:"Bob"    },    {       studentId:103,       studentName:"David"   ... Read More

Is there any way to check if there is a null value in an object or array in JavaScript?

AmitDiwan
Published on 26-Oct-2020 15:42:24
To check if there is a null value in an object or array, use the concept of includes().ExampleFollowing is the code −var nameArray1 = ["John", "David", "Mike", "Sam", null]; if (nameArray1.includes(null) == true) {    console.log("array1 contains null value"); } else {    console.log("array1 does not contains null value"); } ... Read More

Shortest syntax to assign properties from function call to an existing object in JavaScript

AmitDiwan
Published on 26-Oct-2020 15:40:51
For this, use the following −{... anyObjectName1, ...anyObjectName2}.ExampleFollowing is the code −function getTheObject() {    return {       lastName: "Smith",       countryName:"US"    } } var availableObj = { firstName: "Adam" } availableObj = { ...availableObj, ...getTheObject() }; console.log(availableObj);To run the above program, you need to ... Read More

Remove property for all objects in array in JavaScript?

AmitDiwan
Published on 26-Oct-2020 15:39:05
To remove property, use delete keyword in JavaScript.ExampleFollowing is the code −var values = [    {       "firstName": "John",       "lastName":"Smith"    },    {       "firstName": "David",       "lastName":"Miller"    },    {       "firstName": "Adam",     ... Read More
Advertisements