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


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

Updated on: 27-Oct-2020

198 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements