Home > front end >  Can colon (:) be used for assignment in JavaScript?
Can colon (:) be used for assignment in JavaScript?

Time:12-25

I'm new in JavaScript. I was programming and I saw some used

const PI : 3.14

I'm puzzled how ':' notion can be used to assign values to variables. Can anyone explain to me when ':' is used to assign values to variables? That'll be really helpful ^^

CodePudding user response:

If you are just assigning a variable, you can't use a colon. What you want is const PI = 3.14. Colons can be used for other purposes however, for example, setting keys in an object:

const myObject = {
    key: "my new value"
}

CodePudding user response:

NO! That is the wrong syntax and will throw error in JS.

Either you write

const PI = 3.14;

OR

const Obj1 = {PI: 3.14}

These, are the valid syntax in JS.

  • Related