coding180 icon
Coding180
JavaScript - what is a variable?

JavaScript - what is a variable?

Home > Tutorials > JavaScript > Getting Started


From a programming point of view, a variable is a memory used to store certain values. We can regard a variable as a box.

In order to distinguish the boxes, we can use names such as BOX1 and BOX2 to represent different boxes. BOX1 is the name of the box (that is, the name of the variable).

Javascript variable - coding180

Define variables using the keyword var, the syntax is as follows:

var variable name

The variable name can be named arbitrarily, but it must follow the naming rules:

  1. Variables must begin with a letter, an underscore (_), or a dollar sign ($).
  2. Then you can use any number of English letters, numbers, underscores (_) or dollar signs ($).
  3. JavaScript keywords and JavaScript reserved words cannot be used.

JS variable example

where mychar;
mychar="javascript";
var mynum = 6;

Variables can be assigned repeatedly, as follows:

mychar="javascript";
myChar="hello";

Note:

1. JS. For example, the variables mychar and myChar are different, indicating that they are two variables.

2. Although variables can be used directly without declaration, they are not standardized and need to be declared first and then used.

Task - Exercise 7

In line 7, define a variable called myNum and assign it a value of 8.

Use document.write() function to output the value in the variable in line 8

Task Code

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>variable</title>
<script type="text/javascript">
 

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

 


user

Robort Gabriel

Lagos, Nigeria

Freelance Web Developer, Native Android Developer, and Coding Tutor.

Skills
  • UI / UX
  • JavaScript
  • Python
  • PHP
  • Kotlin
  • Java
  • Bootrap
  • Android
  • Laravel