Codewars Basic Variable Assignment JavaScript
Tags: javascript, webdev, beginners, tutorial
Published: 2021-07-22 01:20:48

This article explains how to solve the kata [Basic Variable Assignment](https://www.codewars.com/kata/50ee6b0bdeab583673000025/javascript) from Codewars in JavaScript.
## Instructions
> This code should store `"codewa.rs"` as a variable called name but it's not working. Can you figure out why?
### Provided Code
```javascript
var a == "code";
var b == "wa.rs";
var name == a + b;
```
## Discussion
This kata tests your ability to figure out why the given code isn't working as expected. The goal of the code is to save the string "codewa.rs" to the variable `name`. So we need to figure out why the given code isn't doing that properly.

Don't get distracted by the period in the middle of the string; it doesn't matter and it is not making the tests fail. It's perfectly fine to have a period in the middle of a string, and the tests are expecting the period to be there in between the "a" and the "r".

The key issue at play in this kata is the [assignment operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Assignment) vs the [equality operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#Comparison).

The assignment operator is the equals sign `=`.

The equality operator is the double equals sign `==`. In JavaScript, [there is more than one way to check for equality](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness), but that is a story for another post.

When we want to save a value to a variable, we use the equals sign. When we want to check if two things are equal, we use the double equals sign.

That's what's going wrong! The given code tries to use `==` to assign values to all three variables when it should be using `=`.

### Sidenote
You might be wondering why it's okay to literally add two strings together.

The given code uses the plus sign `+` to **[concatenate](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/concat)**--meaning put together--two strings. It's as simple as that. In JavaScript we can use the plus sign as a shortcut to put strings together.

For example:
```javascript
const greeting = "Hello, ";
const myName = "Dylan";
const sentence = greeting + myName + ".";

console.log(sentence);
// "Hello, Dylan."
```
## Solution 1
In this solution, I chose to replace the `var` keyword with the `const` keyword.

Before ES6, `var` was the only way to declare a variable in JavaScript.

Now, we can use two other keywords `const` and `let`.
`const` is used for variables that are not allowed to be reassigned.

`let` is used for variables that are allowed to be reassigned.

Since none of the variables in this solution are reassigned (meaning given another value using the equals sign) I used `const` to declare them.

I changed all the `==` to `=`, which is the crux of the problem.

```javascript
const a = "code";
const b = "wa.rs";
const name = a + b;
```
## Solution 2
In case you don't feel like concatenating strings directly, you can put together the variables `a` and `b` in this problem with another technique called **string interpolation**. 

Instead of putting two strings together using the plus sign, we can make a **[template string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals)** and embed a JavaScript expression inside the template string.

How do you make a template string? You use backticks instead of quotes. For example:

```javascript
const regularString = "This is a regular string.";
const templateString = `This is a template string.`;
```
Template strings are really useful when you want to insert variables into them. In order to do that, you wrap the variable in curly braces and put a dollar sign in front of it, like this:
```javascript
const myName = "Dylan";
const templateStringWithVariable = `My name is ${myName}.`;

console.log(templateStringWithVariable);
// "My name is Dylan."
```
So we can solve our problem like this:
```javascript
const a = "code";
const b = "wa.rs";
const name = `${a}${b}`;
```

---
Hope you enjoyed this article! You can follow me on [LinkedIn](https://www.linkedin.com/in/dylanattal/) and [GitHub](https://github.com/DylanAttal)!