JavaScript Variables | Var Types | declaration, scope, string

JavaScript Variables (Var) is storage name of datatype. A kind of containter the holding a particuler type of value like String and numbers (integer) etc.

Var” keyword is used to declare Variables in JavaScript. A variable value can change anytime. You can assign the value to variable using equal to (=) operator.

JavaScript Variables

JavasSript Variable Types

There is 2 type of variable by Scope.

  • Local variable
  • Global variable

Other types are based on value assign to that variables – Number, String, boolean, null value, undefined value, object.

javascript variable types

Syntax of JavaScript Variables

Without value JS Variable declaration syntax.

With value

How to do | Javascript Variable Declaration & Initialization

When you just caret a value, it’s called a declaration of the variable. You can assign the value to it later or at the declaration. Let’s see the first how to create the var in javascript.

Let’s create a 2 variable with initial value, first is for numeric value and 2nd one is for string values.

A Javascript variable scope and Types

A globally-scoped Variable

A Variable can be access by any function and members. It’s declared outside any function.

Local scope Variables

Access only within a function, outside function or member can’t access it.

There are more type varible like – Intermediate (let and object), Advanced (Closure and Prototype-based scope resolution), Global+Local,Catch clause-scoped variable.

Follow this link for more info- JS Variable scope

How to Javascript variable in string | Interpolate Variable

Starting from Firefox 34 / Chrome 41 / Safari 9 / Microsoft Edge you can use an ES2015 / ES6 feature called Template Literals and use this syntax:

A complete example

Return sentence of string

JavaScript Variable Name Rules | Guideline

Here is some important point to when you creating a variable.

  • Var Name can have letters (a to z or A to Z), digits (0 to 9), underscores ( _ ), and dollar signs ( $ ).
  • The name must start with a letter, underscore( _ ), or dollar( $ ) sign.
  • Names are case sensitive (a and A are different variables in JS)
  • Reserved words can’t be used as names. Like the “JavaScript” keyword.

Bonus: One Statement, Many Variables

In JavaScript, you can declare a variable as many want in one statement. Using a comma “,” separate between variable name. See below code.

OR, like this

Q: How to display javascript variable value in html page?

Answer: Use Use document.write() and <script> tag in HTML <body>. Let’s see how to use javascript variable in html.

Or, Another example using a function.

There are many ways to display javascript variable value in HTML page, you can do with own experiment.

Q: How to do Javascript print variable?

Answer: You can print the messages on Console or in Alert dialog.

Must read first this tutorial – JavaScript Print to Console | Object.

Q: Can javascript variable declaration without var keyword.

Answer: Yes, you can do the Declaration of the variable in javascript without using the “Var” keyword. But it must assign a value. Like below code.

Note: it not recommended to declare var without using the “Var” keyword, because it can overwrite the existing same name variables. So you can lose the value of variable.

Do comment if you have any doubt or suggestion or questions on this topic.

Note: The JavaScript Variables Examples are tested on Safari browser (Version 12.0.2).
OS: macOS 10.14 Mojave
Version: HECMAScript 2018

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.