javascript – What is the difference between declaring a JSON object with: and =?

Question:

I have always had the doubt that there is a difference between obj1 and obj2

var obj1 = {
  propiedad1: String,
  propiedad2: Boolean,
  propiedad3: Number,
  propiedad4: null,
  propiedad5: "Opcion1" || "Opcion2",
  propiedad6: {
     propiedad1: String,
     propiedad2: Boolean,
  }
}
var obj2 : {
  propiedad1: String,
  propiedad2: Boolean,
  propiedad3: Number,
  propiedad4: null,
  propiedad5: "Opcion1" | "Opcion2",
  propiedad6: {
     propiedad1: String,
     propiedad2: Boolean,
  }
}

When do you use the : and when the = ?

Answer:

The operator = is for assignment in the language while : is for definition in the notation . I explain:

Javascript is a programming language and

When you create an object in programming code, the values can change at any time during execution. To do this, you simply create variables and assign values ​​to them by using = . Example: var x = 2;

On the other hand, JSON is just a notation for information exchange between different languages ​​and platforms and

When you create an object in Json notation, the values ​​will not change once assigned, therefore you are defining them . This is only valid in a JSON string as it is defined that way .

Scroll to Top