Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

66%
+2 −0
Q&A What's the difference between =, == and === operators in JavaScript?

1. = = is an assignment operator in JavaScript, it is used to assign a value to a variable. e.g: const test = 1; console.log(test); // expected output: 1. In the above example, we have ass...

posted 2y ago by Kevin M. Mansour‭  ·  edited 2y ago by Kevin M. Mansour‭

Answer
#3: Post edited by user avatar Kevin M. Mansour‭ · 2021-07-28T05:42:49Z (over 2 years ago)
Cleaning Up.
  • ## 1. `=`.
  • `=` is an assignment operator in JavaScript, it is used to assign a value to a variable. e.g:
  • ```js
  • const test = 1;
  • console.log(test);
  • // expected output: 1.
  • ```
  • In the above example, we have assigned the value `1` to the variable `test` using the assignment operator `=`.
  • Here is an another examples from MDN:
  • ```
  • const x = 2;
  • const y = 3;
  • console.log(x);
  • // expected output: 2.
  • console.log(x = y + 1); // 3 + 1
  • // expected output: 4.
  • console.log(x = x * y); // 4 * 3
  • // expected output: 12.
  • ```
  • Learn more about `=` at [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Assignment).
  • ## 2. `==`.
  • `==` is a comparison operator in JavaScript, it attempts to convert and compare operands that are of different types,
  • returning a boolean result (`true` Or `false`). e.g:
  • ```js
  • console.log(1 == 1);
  • // expected output: true.
  • ```
  • In the above example, we compared `1`, which is a number, to other `1`, which is also a number, that mean the expected output will be `true`
  • since they are the same type, we done this using the comparison operator `==`.
  • Here is an another examples from MDN:
  • ```js
  • console.log('hello' == 'hello');
  • // expected output: true.
  • console.log(1 == "1");
  • // expected output: true.
  • console.log(0 == false);
  • // expected output: true.
  • ```
  • Learn more about `==` at [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Equality).
  • ## 3. `===`.
  • `===` is a strict equality comparison operator in JavaScript, it always considers operands of different types to be different,
  • returning a boolean result (`true` Or `false`). e.g:
  • ```js
  • console.log(true === "true");
  • // expected output: false.
  • ```
  • Since `===` considers operands of different types to be different, so `true` is considered `1` and `"true"` is considered `NaN`,
  • so the expected output will be `false` since they different types, we done this using the strict equality comparison operator `===`.
  • Here is an another examples from MDN:
  • ```js
  • console.log(1 === 1);
  • // expected output: true.
  • console.log('hello' === 'hello');
  • // expected output: true.
  • console.log('1' === 1);
  • // expected output: false.
  • console.log(0 === false);
  • // expected output: false.
  • ```
  • Learn more about `===` at [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality).
  • ## 1. `=`
  • `=` is an assignment operator in JavaScript, it is used to assign a value to a variable. e.g:
  • ```js
  • const test = 1;
  • console.log(test);
  • // expected output: 1.
  • ```
  • In the above example, we have assigned the value `1` to the variable `test` using the assignment operator `=`.
  • Here is an another examples from MDN:
  • ```
  • const x = 2;
  • const y = 3;
  • console.log(x);
  • // expected output: 2.
  • console.log(x = y + 1); // 3 + 1
  • // expected output: 4.
  • console.log(x = x * y); // 4 * 3
  • // expected output: 12.
  • ```
  • Learn more about `=` at [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Assignment).
  • ## 2. `==`
  • `==` is a comparison operator in JavaScript, it attempts to convert and compare operands that are of different types,
  • returning a boolean result (`true` Or `false`). e.g:
  • ```js
  • console.log(1 == 1);
  • // expected output: true.
  • ```
  • In the above example, we compared `1`, which is a number, to other `1`, which is also a number, that mean the expected output will be `true`
  • since they are the same type, we done this using the comparison operator `==`.
  • Here is an another examples from MDN:
  • ```js
  • console.log('hello' == 'hello');
  • // expected output: true.
  • console.log(1 == "1");
  • // expected output: true.
  • console.log(0 == false);
  • // expected output: true.
  • ```
  • Learn more about `==` at [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Equality).
  • ## 3. `===`
  • `===` is a strict equality comparison operator in JavaScript, it always considers operands of different types to be different,
  • returning a boolean result (`true` Or `false`). e.g:
  • ```js
  • console.log(true === "true");
  • // expected output: false.
  • ```
  • Since `===` considers operands of different types to be different, so `true` is considered `1` and `"true"` is considered `NaN`,
  • so the expected output will be `false` since they different types, we done this using the strict equality comparison operator `===`.
  • Here is an another examples from MDN:
  • ```js
  • console.log(1 === 1);
  • // expected output: true.
  • console.log('hello' === 'hello');
  • // expected output: true.
  • console.log('1' === 1);
  • // expected output: false.
  • console.log(0 === false);
  • // expected output: false.
  • ```
  • Learn more about `===` at [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality).
  • ## Summarize
  • 1. `=` is an assignment operator.
  • 2. `==` is a comparison operator.
  • 3. `===` is a strict equality comparison operator.
#2: Post edited by user avatar Kevin M. Mansour‭ · 2021-07-28T05:25:00Z (over 2 years ago)
Minor Stuff
  • ## 1. `=`.
  • `=` is an assignment operator in JavaScript, it is used to assign a value to a variable. e.g:
  • ```js
  • const test = 1;
  • console.log(test);
  • // expected output: 1.
  • ```
  • In the above example, we have assigned the value `1` to the variable `test` using the assignment operator `=`.
  • Here is an another examples from MDN:
  • ```
  • const x = 2;
  • const y = 3;
  • console.log(x);
  • // expected output: 2.
  • console.log(x = y + 1); // 3 + 1
  • // expected output: 4.
  • console.log(x = x * y); // 4 * 3
  • // expected output: 12.
  • ```
  • Learn more about `=` at [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Assignment).
  • ## 2. `==`.
  • `==` is a comparison operator in JavaScript, it attempts to convert and compare operands that are of different types,
  • returning a boolean result (`true` Or `false`). e.g:
  • ```js
  • console.log(1 == 1);
  • // expected output: true.
  • ```
  • In the above example, we compared `1`, which is a number, to other `1`, which is also a number, that mean the expected output will be `true`
  • since they are the same type, we done this using the comparison operator `==`.
  • Here is an another examples from MDN:
  • ```js
  • console.log('hello' == 'hello');
  • // expected output: true.
  • console.log(1 == "1");
  • // expected output: true.
  • console.log(0 == false);
  • // expected output: true.
  • ```
  • Learn more about `==` at [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Equality).
  • ## 3. `===`.
  • `===` is a strict equality comparison operator in JavaScript, it always considers operands of different types to be different,
  • returning a boolean result(`true` Or `false`). e.g:
  • ```js
  • console.log(true === "true");
  • // expected output: false.
  • ```
  • Since `===` considers operands of different types to be different, so `true` is considered `1` and `"true"` is considered `NaN`,
  • so the expected output will be `false` since they different types, we done this using the strict equality comparison operator `===`.
  • Here is an another examples from MDN:
  • ```js
  • console.log(1 === 1);
  • // expected output: true.
  • console.log('hello' === 'hello');
  • // expected output: true.
  • console.log('1' === 1);
  • // expected output: false.
  • console.log(0 === false);
  • // expected output: false.
  • ```
  • Learn more about `===` at [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality).
  • ## 1. `=`.
  • `=` is an assignment operator in JavaScript, it is used to assign a value to a variable. e.g:
  • ```js
  • const test = 1;
  • console.log(test);
  • // expected output: 1.
  • ```
  • In the above example, we have assigned the value `1` to the variable `test` using the assignment operator `=`.
  • Here is an another examples from MDN:
  • ```
  • const x = 2;
  • const y = 3;
  • console.log(x);
  • // expected output: 2.
  • console.log(x = y + 1); // 3 + 1
  • // expected output: 4.
  • console.log(x = x * y); // 4 * 3
  • // expected output: 12.
  • ```
  • Learn more about `=` at [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Assignment).
  • ## 2. `==`.
  • `==` is a comparison operator in JavaScript, it attempts to convert and compare operands that are of different types,
  • returning a boolean result (`true` Or `false`). e.g:
  • ```js
  • console.log(1 == 1);
  • // expected output: true.
  • ```
  • In the above example, we compared `1`, which is a number, to other `1`, which is also a number, that mean the expected output will be `true`
  • since they are the same type, we done this using the comparison operator `==`.
  • Here is an another examples from MDN:
  • ```js
  • console.log('hello' == 'hello');
  • // expected output: true.
  • console.log(1 == "1");
  • // expected output: true.
  • console.log(0 == false);
  • // expected output: true.
  • ```
  • Learn more about `==` at [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Equality).
  • ## 3. `===`.
  • `===` is a strict equality comparison operator in JavaScript, it always considers operands of different types to be different,
  • returning a boolean result (`true` Or `false`). e.g:
  • ```js
  • console.log(true === "true");
  • // expected output: false.
  • ```
  • Since `===` considers operands of different types to be different, so `true` is considered `1` and `"true"` is considered `NaN`,
  • so the expected output will be `false` since they different types, we done this using the strict equality comparison operator `===`.
  • Here is an another examples from MDN:
  • ```js
  • console.log(1 === 1);
  • // expected output: true.
  • console.log('hello' === 'hello');
  • // expected output: true.
  • console.log('1' === 1);
  • // expected output: false.
  • console.log(0 === false);
  • // expected output: false.
  • ```
  • Learn more about `===` at [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality).
#1: Initial revision by user avatar Kevin M. Mansour‭ · 2021-07-28T05:21:59Z (over 2 years ago)
## 1. `=`.

`=` is an assignment operator in JavaScript, it is used to assign a value to a variable. e.g:

```js
const test = 1;

console.log(test);
// expected output: 1.
```

In the above example, we have assigned the value `1` to the variable `test` using the assignment operator `=`.

Here is an another examples from MDN:

```
const x = 2;
const y = 3;

console.log(x);
// expected output: 2.

console.log(x = y + 1); // 3 + 1
// expected output: 4.

console.log(x = x * y); // 4 * 3
// expected output: 12.
```

Learn more about `=` at [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Assignment).

## 2. `==`.

`==` is a comparison operator in JavaScript, it attempts to convert and compare operands that are of different types,
returning a boolean result (`true` Or `false`). e.g:
 
```js
console.log(1 == 1);
// expected output: true.
```

In the above example, we compared `1`, which is a number, to other `1`, which is also a number, that mean the expected output will be `true` 
since they are the same type, we done this using the comparison operator `==`.

Here is an another examples from MDN:

```js
console.log('hello' == 'hello');
// expected output: true.

console.log(1 == "1");
// expected output: true.

console.log(0 == false);
// expected output: true.
```

Learn more about `==` at [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Equality).

## 3. `===`.

`===` is a strict equality comparison operator in JavaScript, it always considers operands of different types to be different, 
returning a boolean result(`true` Or `false`). e.g:

```js
console.log(true === "true");
// expected output: false.
```

Since `===` considers operands of different types to be different, so `true` is considered `1` and `"true"` is considered `NaN`, 
so the expected output will be `false` since they different types, we done this using the strict equality comparison operator `===`.

Here is an another examples from MDN:

```js
console.log(1 === 1);
// expected output: true.

console.log('hello' === 'hello');
// expected output: true.

console.log('1' ===  1);
// expected output: false.

console.log(0 === false);
// expected output: false.
```

Learn more about `===` at [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality).