https://github.com/codeadamca/javascript-variables-arrays
A basic example of using variables and arrays.
https://github.com/codeadamca/javascript-variables-arrays
javascript learning-code variables
Last synced: 2 months ago
JSON representation
A basic example of using variables and arrays.
- Host: GitHub
- URL: https://github.com/codeadamca/javascript-variables-arrays
- Owner: codeadamca
- Created: 2021-01-13T01:42:30.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2025-01-26T22:08:43.000Z (12 months ago)
- Last Synced: 2025-01-26T23:18:45.407Z (12 months ago)
- Topics: javascript, learning-code, variables
- Language: HTML
- Homepage:
- Size: 240 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# A Basic Introduction to JavaScript, Variables, and Arrays
Variables and arrays are used to store data to use within your code. A variable can store one piece of data whle an array can store multiple values.
## Using Variables
To create a variable use the `var`, `let`, or `const` commands.
Open up a new HTML file, name it variables.html, add the standard HTML elements, and then add the following code to the `body` tag:
```html
var firstName = "Peter";
var lastName = "Smith";
document.write( "<p>Hello " + firstName + " " + lastName + "</p>" );
```
Inside a `document.write()` command, text in between quotes is treated as plain text, while text outside of quotes is treated as a variable, array, or other JavaScript command.
Open the the file using a browser. The `body` section should include a greeting.
## Trying it Out
Create an HTML file and add the following code:
```html
JavaScript Variables Exercise
var linkName = "W3Schools";
var linkLogo = "w3schools.png";
var linkURL = "http://www.w3schools.com/";
var linkDescription = "W3Schools is a web developer information website, with tutorials and references relating to web development topics such as HTML, CSS, JavaScript and PHP.";
```
Use `document.write()` and the provided variables to display the link information using well formatted HTML. For example, the first variable may look like this:
```html
JavaScript Variables Exercise
var linkName = "W3Schools";
var linkLogo = "w3schools.png";
var linkURL = "http://www.w3schools.com/";
var linkDescription = "W3Schools is a web developer information website, with tutorials and references relating to web development topics such as HTML, CSS, JavaScript and PHP.";
document.write("<h1>" + linkName + "</h1>");
```
When you have completed the goal, the `body` tag will only include one `script` tag, the four provided variables, and a number of document.write()` commands outputting the variables with HTML.
## Varables and Math
Open up a new HTML file, name it variables.html, add the standard HTML elements, and then add the following code to the `body` tag:
```html
var x = 5;
var y = 10;
document.write( "<p>Five plus ten is " + x + y + "</p>" );
document.write( "<p>Five times ten is " + x * y + "</p>" );
```
Open the the file using a browser. The `body` section should contain a series of math answers.
## Using Arrays
To create an array, use the same syntax as a variable, except the type of variable with be an array:
```javascript
var colours = new Array();
```
Then populate the array using keys:
```javascript
colours[0] = 'Red';
colours[1] = 'Blue';
colours[2] = 'Green';
```
Outputting an array value is similar to outputting a variable, except the variable is followed by a key:
```javascript
document.write("
My favourtie colour is " + colours[0] + ".
");
```
## Trying it Out
Create an array list of items. Then use a series of `document.write()` commands to display the list.
> Full tutorial URL:
> https://codeadam.ca/learning/javascript-variables-arrays.html
***
## Repo REsources
* [Visual Studio Code](https://code.visualstudio.com/)