JavaScript Variables

One of the most fundamental characteristics of a programming language is the set of data types it supports. These are the type of values that can be represented and manipulated in a programming language.

  • Number
  • String
  • Boolean
  • JavaScript Variables

    Like many other programming languages, JavaScript has variables. Variables can be thought of as named containers. You can place data into these containers and then refer to the data simply by naming the container. Before you use a variable in a JavaScript program, you must declare it. Variables are declared with the var keyword.

    Note : JavaScript is untyped language. This means that a JavaScript variable can hold a value of any data type. Unlike many other languages, you don't have to tell JavaScript during variable declaration what type of value the variable will hold. The value type of a variable can change during the execution of a program and JavaScript takes care of it automatically.

    You can also declare multiple variables with the same var keyword

    Storing a value in a variable is called variable initialization. You can do variable initialization at the time of variable creation or at a later point in time when you need that variable.

    JavaScript Variable Scope

    The scope of a variable is the region of your program in which it is defined.

  • Global Variables:

    A global variable has global scope which means it can be defined anywhere in your JavaScript code.

  • Local Variables :

    A local variable will be visible only within a function where it is defined. Function parameters are always local to that function.

  • Within the body of a function, a local variable takes precedence over a global variable with the same name. If you declare a local variable or function parameter with the same name as a global variable, you effectively hide the global variable.

    Example :-