JavaScript tutorials for Beginners – JavaScript Variables and Constants

 

 

(JavaScript Tutorials for Beginners)

In this end-to-end example, you will learn – JavaScript tutorials for Beginners – JavaScript Variables and Constants.

JavaScript Variables and Constants

In this tutorial, you will learn about JavaScript variables and constants, and also how to initialize and use them with the help of examples.

JavaScript Variables

In programming, a variable is a container (storage area) to hold data.


JavaScript Declare Variables

In JavaScript, we use the following keywords to declare variables: var and let. For example,

var x;
let y;

Here, x and y are variables.


JavaScript var Vs let

Both var and let are used to declare variables. However, there are some differences between them.

var let
var is used in the older versions of JavaScript let is the new way of declaring variables starting ES6 (ES2015).
var is function scoped (will be discussed in the later tutorials). let is block scoped (will be discussed in the later tutorials).
For example, var x; For example, let y;

Note: It is recommended to use let instead of var. However, there are a few browsers that do not support let.


JavaScript Initialize Variables

We use the assignment operator = to assign a value to a variable.

let x;
x = 5;

Here, 5 is assigned to variable x.

You can also initialize variables during its declaration.

let x = 5;
let y = 6;

In JavaScript, it’s possible to declare variables in a single statement.

let x = 5, y = 6, z = 7;

If you use a variable without initializing it, it will have an undefined value.

let x; // x is the name of the variable

console.log(x); // undefined

Here x is the variable name and since it does not contain any value, it will be undefined.

You will learn about undefined and other data types in the next tutorial in detail.


Change the Value of Variables

It’s possible to change the value stored in the variable. For example,

// 5 is assigned to variable x
let x = 5; 
console.log(x); // 5

// variable x is changed 3
x = 3; 
console.log(x); // 3

The value of a variable may vary. Hence, the name variable.


Rules for Naming JavaScript Variables

The rules for naming variables are:

  1. Variable names must start with either a letter, an underscore _, or the dollar sign $. For example,
    //valid
    let a = 'hello';
    let _a = 'hello;
    let $a = 'hello';
  2. Variable names cannot start with numbers. For example,
    //invalid
    Let 1a = 'hello'; // this gives an error
  3. JavaScript is case-sensitive. So y and Y are different variables. For example,
    let y = "hi";
    let Y = 5;
    
    console.log(y); // hi
    console.log(Y); // 5
  4. Keywords cannot be used as variable names. For example,
    //invalid
    let new = 5; // Error! new is a keyword.

Notes:

  • Though you can name variables in any way you want, it’s a good practice to give a descriptive variable name. If you are using a variable to store the number of apples, it better to use apples or numberOfApples rather than x or n.
  • In JavaScript, the variable names are generally written in camelCase if it has multiple words. For example, firstNameannualSalary, etc.

JavaScript Constants

The const keyword was also introduced in the ES6(ES2015) version to create constants. For example,

const x = 5;

Once a constant is initialized, we cannot change its value.

const x = 5;
x = 10;  // Error! constant cannot be changed.
console.log(x)

Simply, a constant is a type of variable whose value cannot be changed.

Also, you cannot declare a constant without initiating it. For example,

const x;  // Error! Missing initializer in const declaration.
x = 5;
console.log(x)

Note: If you are sure that the value of a variable won’t change throughout the program, it’s recommended to use const. However, there are a few browsers that do not support const.


Now that you know about variables, you will learn about different types of data a variable can store in the next tutorial.

 

Personal Career & Learning Guide for Data Analyst, Data Engineer and Data Scientist

Applied Machine Learning & Data Science Projects and Coding Recipes for Beginners

A list of FREE programming examples together with eTutorials & eBooks @ SETScholars

95% Discount on “Projects & Recipes, tutorials, ebooks”

Projects and Coding Recipes, eTutorials and eBooks: The best All-in-One resources for Data Analyst, Data Scientist, Machine Learning Engineer and Software Developer

Topics included: Classification, Clustering, Regression, Forecasting, Algorithms, Data Structures, Data Analytics & Data Science, Deep Learning, Machine Learning, Programming Languages and Software Tools & Packages.
(Discount is valid for limited time only)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.

Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners

Please do not waste your valuable time by watching videos, rather use end-to-end (Python and R) recipes from Professional Data Scientists to practice coding, and land the most demandable jobs in the fields of Predictive analytics & AI (Machine Learning and Data Science).

The objective is to guide the developers & analysts to “Learn how to Code” for Applied AI using end-to-end coding solutions, and unlock the world of opportunities!

 

Excel formula for Beginners – How to Count cells equal to one of many things

C++ for Beginners: C++ Storage Class

JavaScript tutorials for Beginners – JavaScript Function and Function Expressions