A variable is a name that is assigned a value that can be used at any time within our javascript code.
It starts with the reserved word "var", "let" or "const" followed by our variable name, equal to the value we want to give it between quotes. Always ending with a ";".
For example:
var text = "this is a message";
To use this message you simply have to write the name of the variable at the time you want.
That is, in this case, you simply have to write "text" at the time you want.
In the following example we declare the numeric variable var_num, assign it a value and declare it inside console.log to display its content inside the browser console.
const name_var = 18;
console.log(name_var);
Variables can be of type:
Generally our variables have to be defined at the beginning of our code, so that later they can be used at the time we want.
We define the variable types as follows:
Code example with variables without errors:
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div>TODO write content</div>
<script type="text/javascript">
console.log(text2);
const name = "myName";
let text = 'myLetText';
var text2 = 'myVarText';
console.log(name);
console.log(text);
console.log(text2);
</script>
</body>
</html>
The definition of the name of the variables in javascript is always done as follows:
let nameVariable = 'this is my var';
let name = 'this is my var';
Tips on SEO and Online Business
Next Articles
Previous Articles