In this post, I will tell you what is variables in Javascript and how to declare variables in Javascript and pass values like numbers, strings, etc.
Variables in Javascript
let myName = "codeyup"; console.log(myName);
Output: codeyup
In the above example, I have declared a variable (myName) using let whose value is codeyup and it is a string. You will read more about var, let, and const in the next blog posts.
let a = 21; let b = 39; console.log(a + b);
Output: 60
In the above example, I have declared two variables (a and b) whose values are 21 and 39. These are Number data types.
Note: Values that we write in ” “ are treated as a string value. Suppose I write “4” then it is a string data type.
let a = "2"; //It is a string data type let b = 2; //it is a number data type
Ways to declare variables in Javascript
These are some ways to declare variables in Javascript
1. Camel case
let myAddress;
You can also write the first alphabet in capital if you want to write in that way.
2. Underscore
let my_Address;
You can write the second word by using an underscore.
3. Using the $ symbol
let my$Address;
You can write the second word by using an $ symbol.
Note:
When declaring a variable in javascript, do not use the reserved Javascript keywords for declaring a variable.
For example:
let function = 160;
This way of declaring a variable is not allowed in Javascript. But you can declare it by adding $ at the beginning. See the below code.
let $function = 20; console.log($function);
This way you can declare it. The output of the above is 20.