Javascript-variable
Table of contents
Hello All, Welcome back here. In Last Episode we have seen a bit details about what is javascript and why it is. Today we are going through a lesson inside the Javascript. Yeah Let's Just Sit Back Read Back and Enjoy.
Well, Yes. We All came through several social network like what's app ,Instagram and it Goes on. We tend to use these on main cause of killing our unexciting time by chatting with random people or know one.Have you ever surprised of, where all your information get stored and transfused? No Guess,Right! yup everything is binded or stored through a variable.
A variable is a “named storage” for data. We can use variables to store goodies, visitors, and other data.
In Java Script, to create a variable you need a keyword "let" or "var" before your variable name
for example, ```let myName = 'Joey'
Here, myName refer's our variable name and 'Joey' is the our variable value. 'Joey' is a string data.
We can access the Joey by the variable name.
We can also Declare multiple variables in a single line,
```let friendOne = 'Chandler' , friendTwo = 'Ross' , friendThree = 'Joey'
Up here we've declared three variables with single let keyword
```let friendOne = 'Chandler', friendTwo = 'Ross', friendThree = 'Joey';
```let friendOne = 'Chandler'
, friendTwo = 'Ross'
, friendThree = 'Joey';
everything works in a sameway, it's matter of one's personal choice. Anything is Fine
Secondly, > var It's always ambigous when it come's for the difference between let and var, people thing they are same, but they are not. 'var' is just a old school method of declaring a variable.
``````var myName = 'Joey'
this is as same as the
```let myName = 'Joey'
let's not get confuse with it now. We'll see this about in detail
PROTOCOL
There are few rules while declaring or creating a variable. They are,
1.The name must contain only letters, digits, or the symbols $ and _. 2.The first character must not be a digit.
That's it, Done.
VARIABLE NAMING CONVICTIONS
These names are valid:
let $ = 1; // declared a variable with the name "$" let = 2; // and now a variable with the name ""
These names are invalid:
let 1a; // cannot start with a digit let my-name; // hyphens '-' aren't allowed in the name
Case matters Variables named apple and APPLE are two different variables.
Non-Latin letters are allowed, but not recommended It is possible to use any language, including cyrillic letters or even hieroglyphs, like this:
let имя = '...'; let 我 = '...';
Reserved names There is a list of reserved words, which cannot be used as variable names because they are used by the language itself.
For example: let, class, return, and function are reserved.
The code below gives a syntax error:
let let = 5; // can't name a variable "let", error! let return = 5; // also can't name it "return", error!
Another interesting one in Js is, Without using the keyword var or let actually you can declare a variable
```name ='Joey'
console.log(name) // will be Joey ```
this is an old method. To avoid these conflicts strict is used. But they are not a good practice
And tertiary there is another way of declaring a variable, and that is const.
const is a keyword that is used to declare a variable. But it stand out odd from 'let' and 'var' because only a variable is declared with the const keyword, the values cannot be reassigned.It's fixed, and when you try to change the value it will throw an error.
const are used in your code, for the values you've known it prior of execution and values that does'nt change throughout the code.
TIP
Some good-to-follow rules are:
Use human-readable names like userName or shoppingCart. Stay away from abbreviations or short names like a, b, c, unless you really know what you’re doing. Make names maximally descriptive and concise. Examples of bad names are data and value. Such names say nothing. It’s only okay to use them if the context of the code makes it exceptionally obvious which data or value the variable is referencing. Agree on terms within your team and in your own mind. If a site visitor is called a “user” then we should name related variables currentUser or newUser instead of currentVisitor or newManInTown.
And we are at the end of this chapter. Hope you,re not bored enough.
ps : comment your suggestion on anything that might help my tomorrow with better comeup
Grateful for Your Time
Subscribe to my newsletter
Read articles from SAM R directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by