Home

Javascript basics

What is Javascript?

HTML and CSS are called markup languages. They describe certain elements of a document. Javascript is a programming language. It allows you to change elements on the page based on user actions. Just like HTML and CSS, you can write javascript in notepad, and it will run in your browser.

The language

Javascript has some qualities that are different from CSS and HTML that are part of most programming languages. We'll discuss two of them, variables and functions.

Variables

A variable is something you define in your program that can store a number or word that can change over time. Think of it like a backpack. You might have a computer in a backpack, or a shirt, or some food. you can change the contents whenever you like. Variables are used by programmers to store and change data over time. You can refer to a variable by its name to retrieve the information, or to set the information.

You write a variable in javascript like this

var myVariableName = someValue ; 

Functions

Functions are pieces of code that do something. When you write a function, it becomes a collection of code that you can reuse over and over again. You invoke a function by calling its name when an event happens. Web events might be selecting a button, loading a page, or entering text into a field.

You write a function in javascript like this


function myFunction() {
}

Your code to execute a change in the page would go inside the braces.

Accessing parts of a webpage

To change parts of a webpage, javascript has a way to talk to the elements in html. It can address elements by calling their name from a hierarchy that outlines the page's html elements, or through an id attribute. Think of it as javascript giving the webpage a shoulder tap and asking it to change something. You can change the content of any attribute or element in a page including text and styles.

Javascript example 1