Use variable as the property name in JavaScript

Description

The following code shows how to use variable as the property name.

Example


<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var tutorial = {<!--from   ww w  . j ava 2  s .  c o m-->
name : "JavaScript",
pageSize : 9
};

var propertyName = "name";
document.writeln(tutorial[propertyName]); //"JavaScript"

</script>
</head>
<body>
</body>
</html>

Click to view the demo

The code above generates the following result.

Use variable as the property name in JavaScript