본문으로 건너뛰기

Variable Declaration Keyword - var

Variable Declaration Keyword - var

What is var?

var is a keyword used to declare variables in JavaScript.

A variable is a named storage that can store data to be referenced or changed later.

var creates a space to store a specific value and retrieve it for use when needed.


Basic Usage

When declaring a variable, use the var keyword, specify the variable name, and then you can assign a value to it.

Example:

var studentName = 'Jimin';

Here, studentName is the name of the variable, and "Jimin" is the value stored in that variable.


Features of Variables

Reassignable: A var variable once declared can be reassigned to a different value.

var number = 10;
number = 20; // The value of variable number is changed to 20.

Function-level scope: var has a function-level scope.

This means that a var variable declared inside a function is only accessible within that function.

However, a var variable declared outside of a function becomes a global variable and can be used anywhere.

Example:

function myFunction() {
var insideFunction = 'Hello!';
}

// The variable insideFunction is not accessible here.

Other Variable Declaration Keywords

In modern JavaScript, in addition to var, there are two more keywords for declaring variables: let and const.

These have different characteristics compared to var, so it's important to choose wisely which keyword to use in different situations.