How to recognize when to use : or = in JavaScript?

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

The colon (:) can be used when you want to define the property to an object while equal (=) can be used when you want to assign a value to a variable.

Example

Following is the code −

var studentDetails = {
   "studentId": 101,
   "studentName": "John",
   "studentSubjectName": "Javascript",
   "studentCountryName": "US"
}
console.log(studentDetails);
var firstName = "David";
console.log(firstName);

To run the above program, you need to use the below command −

node fileName.js.

Here, my file name is demo325.js.

Output

This will produce the following output −

PS C:\Users\Amit\javascript-code> node demo325.js
{
   studentId: 101,
   studentName: 'John',
   studentSubjectName: 'Javascript',
   studentCountryName: 'US'
}
David
raja
Published on 27-Oct-2020 10:03:00
Advertisements