Introduction to Javascript

What is JavaScript?

JavaScript is the world's most popular programming language. It is a full-fledged dynamic programming language that can add interactivity to a website. It was invented by Brendan Eich (co-founder of the Mozilla project, the Mozilla Foundation, and the Mozilla Corporation).

JavaScript is versatile and beginner-friendly. With more experience, you'll be able to create games, animated 2D and 3D graphics, comprehensive database-driven apps, and much more!

JavaScript itself is relatively compact, yet very flexible. Developers have written a variety of tools on top of the core JavaScript language, unlocking a vast amount of functionality with minimum effort. These include:

JavaScript and Java

JavaScript and Java are similar in some ways but fundamentally different in some others. The JavaScript language resembles Java but does not have Java's static typing and strong type checking. JavaScript follows most Java expression syntax, naming conventions and basic control-flow constructs which was the reason why it was renamed from LiveScript to JavaScript.

In contrast to Java's compile-time system of classes built by declarations, JavaScript supports a runtime system based on a small number of data types representing numeric, Boolean, and string values. JavaScript has a prototype-based object model instead of the more common class-based object model. The prototype-based model provides dynamic inheritance; that is, what is inherited can vary for individual objects. JavaScript also supports functions without any special declarative requirements. Functions can be properties of objects, executing as loosely typed methods.

JavaScript is a very free-form language compared to Java. You do not have to declare all variables, classes, and methods. You do not have to be concerned with whether methods are public, private, or protected, and you do not have to implement interfaces. Variables, parameters, and function return types are not explicitly typed.

Hello World

To get started with writing JavaScript, write your first "Hello world" JavaScript code:

function greetMe(yourName) { alert("Hello " + yourName); } greetMe("World");

Statements

A script is a series of instructions that a computer can follow one-by-one. Each individual instruction or step is known as a statement. Statements end with a semicolon ;. Statements are executed one by one, in the same order as they are written. Example:

a = 5; b = 6; c = a + b;

Code Blocks

Java script statements can be grouped together in code blocks, insue curly brackets { }. Code blocks define statements to be executed together.One place you will find statements grouped together in blocks, is in JavaScript functions:

function myFunction () { document.getElementById("demo1").innerHTML = "Hello Dolly!";
documentß.getElementById("demo2").innerHTML = "How are you?";
}

Variables

A variable is a container for a value, like a number we might use in a sum, or a string that we might use as part of a sentence. But one special thing about variables is that their contained values can change. You can declare a variable in three ways.

  1. With the keyward var. This Syntac can be used to declar both local and global variables.

    var x = 42

  2. Assigning it a value. This always declares a global variable. It generates a strict JavaScript warning. You shouldn't use this variant.

    x = 42

  3. With the keyword let. This syntax can be used to declare a block scope local variable. See Variable scope below.
    let y = 13

Data Types

Data types speficy what kind of data can be stored and manipulated within a program. There are six basic data types in JavaScript which can be divied into three main cateogories: primitive (or primary), composite (or reference), and special data types.

Strings

String data types consists of letters and other characters. Strings are written with either single or double quotes.
var carName1 = "Volvo XC60"; // Using double quotes
var carName2 = 'Volvo XC60'; // Using single quotes

Numbers

Numbers can be written with, or without decimals and they can also be written as negative numbers.
var x1 = 34.00; // Written with decimals
var x2 = 34; // Written without decimals

Booleans

Boolean data types can have one of two values: true or false.

var x = 5; var y = 5; var z = 6;
(x == y) // Returns true
(x == z) // Returns false

Arrays

Arrays are special types of variables. It stores a list of values. Arrays are written with square brackets and array items are seperated by commas. Use arrays whenever you are working with a list or a set of values that are related to each other.
var cars = ["Saab", "Volvo", "BMW"];

Objects

Objects are written with curly braces {}.

Undefined

A variable without a value has the value of undefined. Type is also unefined.
car = undefined; // Value is undefined, type is undefined

Null

In JavaScript null is "nothing". It is supposed to be something that doesn't exist. Unfortunately, in JavaScript, the data type of null is an object.