1. javaScript

Variables in JavaScript

Any programming languages is based upon certain entities upon which it performs actions and get things done. It’s same like a food recipe depends upon many ingredients for it’s existence. Similarly any Software/App/Website’s being depends upon certain entities used to defined aspects of it.

How to declare a variable in JavaScript? JavaScript use var keyword with a name to declare variable. Not only this, value to a variable can be assigned using equal(=).

var programming;  //declaring a JavaScript variable

var programming = "I want to learn programming"; 
//declaring and assigning value at same time

Both of these ways are appropriate and you can use any one of these for declaring a variable in JavaScript.

Best practices/rules for declaring variable names in JavaScript

I’ve been part of a Website Development team, while working for as a Freelancer JavaScript Developer specifically developing website using JavaScript. Prior to that work, when I was just learning the JavaScript Language I used to just name give some random names to variables without considering anything.

But the work which I did while being in that team made me realise that I need to learn more about Variable names; like what are JavaScript best practices for declaring Variable Names? Then I researched a lot, read a lot of books/reached out to other people on coding platforms like stack-overflow and learned a lot about Variable naming in JavaScript.

Here are some of best suggestions/javaScript naming conventions, which I would like you to follow while writing JavaScript code –

  • First character should be letter/underscore
  • Don’t use number as first character
  • If variable name start with letter/underscore, not with number then other characters of variable name can be anything
  • JavaScript doesn’t put a limit to length of variable
  • JavaScript have certain reserved words, void using those as variable names.

If your not aware of What are Reserved Words in JavaScript? then you don’t need to worry as I’ve put together an amazing table listing all of Reserved Words in JavaScript. You can see that table here – Reserved Words in JavaScript Computer Science Hub.

JavaScript Variables Names
website (Good variable name)
_website (Don’t use such variable names – Starting with underscore)
10website (Don’t use variable names – starting with number)
website_name (Good variable name)
website10 (Good variable name)
Never use a Reserved Word name in JavaScript as Variable name
Examples of Valid JavaScript Variables Names

Are JavaScript variables strictly types?

Clearly not, JavaScript doesn’t require developer to explicitly mention Data Type of a variable, this feature of JavaScript allows flexible value assigning to variables. For example – Somewhere in code you can say var number = 10; and at some other point you can say number = “ten”; this would be totally fine and JavaScript will not throw any error.

  • var website_name = “Computer Science Hub”;
  • website_name = “Google”;
  • Doing these reassigning is totally fine

Final suggestions regarding JavaScript Variable names

Finally from almost 1 year of JavaScript Developer experience that I’ve, I would recommend you just try to stick to JavaScript Variable Naming Conventions which I listed above. Moreover as you start to work in teams and write more code then your skill of naming variables appropriately would improve.

If in case you’ve any question regarding JavaScript variables, or just want to ask anything about JavaScript then come below and leave a comment. I would try to reply as soon as possible.

Moreover If your learning JavaScript Development, I would recommend you to go through other articles about JavaScript on this website. You can find other JavaScript articles here – JavaScript Computer Science Hub.

Comments to: Variables in JavaScript

Your email address will not be published.