All pages
Powered by GitBook
1 of 5

Loading...

Loading...

Loading...

Loading...

Loading...

Comments

Comments are statements that will not be executed by the interpreter, comments are used to mark annotations for other programmers or small descriptions of what your code does, thus making it easier for others to understand what your code does.

In Javascript, comments can be written in 2 different ways:

  • Line starting with //:

// This is a comment, it will be ignored by the interpreter
var a = "this is a variable defined in a statement";
  • Section of code starting with /*and ending with */, this method is used for multi-line comments:

/*
This is a multi-line comment,
it will be ignored by the interpreter
*/
var a = "this is a variable defined in a statement";

Mark the editor's contents as a comment

Mark me as a comment
or I'll throw an error
/*
Mark me as a comment
or I'll throw an error
*/
assert(true);

Basics

In this first chapter, we'll learn the basics of programming and the Javascript language.

Programming means writing code. A book is made up of chapters, paragraphs, sentences, phrases, words and finally punctuation and letters, likewise a program can be broken down into smaller and smaller components. For now, the most important is a statement. A statement is analogous to a sentence in a book. On its own, it has structure and purpose, but without the context of the other statements around it, it isn't that meaningful.

A statement is more casually (and commonly) known as a line of code. That's because statements tend to be written on individual lines. As such, programs are read from top to bottom, left to right. You might be wondering what code (also called source code) is. That happens to be a broad term which can refer to the whole of the program or the smallest part. Therefore, a line of code is simply a line of your program.

Here is a simple example:

This code can be executed by another program called an interpreter that will read the code, and execute all the statements in the right order.

var hello = "Hello";
var world = "World";

// Message equals "Hello World"
var message = hello + " " + world;

Variables

The first step towards really understanding programming is looking back at algebra. If you remember it from school, algebra starts with writing terms such as the following.

3 + 5 = 8

You start performing calculations when you introduce an unknown, for example x below:

3 + x = 8

Shifting those around you can determine x:

x = 8 - 3
-> x = 5

When you introduce more than one you make your terms more flexible - you are using variables:

x + y = 8

You can change the values of x and y and the formula can still be true:

x = 4
y = 4

or

x = 3
y = 5

The same is true for programming languages. In programming, variables are containers for values that change. Variables can hold all kind of values and also the results of computations. Variables have a name and a value separated by an equals sign (=). Variable names can be any letter or word, but bear in mind that there are restrictions from language to language of what you can use, as some words are reserved for other functionality.

Let's check out how it works in Javascript, The following code defines two variables, computes the result of adding the two and defines this result as a value of a third variable.

var x = 5;
var y = 6;
var result = x + y;

Equality

Programmers frequently need to determine the equality of variables in relation to other variables. This is done using an equality operator.

The most basic equality operator is the == operator. This operator does everything it can to determine if two variables are equal, even if they are not of the same type.

For example, assume:

var foo = 42;
var bar = 42;
var baz = "42";
var qux = "life";

foo == bar will evaluate to true and baz == qux will evaluate to false, as one would expect. However, foo == baz will also evaluate to true despite foo and baz being different types. Behind the scenes the == equality operator attempts to force its operands to the same type before determining their equality. This is in contrast to the === equality operator.

The === equality operator determines that two variables are equal if they are of the same type and have the same value. With the same assumptions as before, this means that foo === bar will still evaluate to true, but foo === baz will now evaluate to false. baz === qux will still evaluate to false.

Types

Computers are sophisticated and can make use of more complex variables than just numbers. This is where variable types come in. Variables come in several types and different languages support different types.

The most common types are:

  • Numbers

    • Float: a number, like 1.21323, 4, -33.5, 100004 or 0.123

    • Integer: a number like 1, 12, -33, 140 but not 1.233

  • String: a line of text like "boat", "elephant" or "damn, you are tall!"

  • Boolean: either true or false, but nothing else

  • Arrays: a collection of values like: 1,2,3,4,'I am bored now'

  • Objects: a representation of a more complex object

  • null: a variable that contains null contains no valid Number, String, Boolean, Array, or Object

  • undefined: the undefined value is obtained when you use an object property that does not exist, or a variable that has been declared, but has no value assigned to it.

JavaScript is a “loosely typed” language, which means that you don't have to explicitly declare what type of data the variables are. You just need to use the var keyword to indicate that you are declaring a variable, and the interpreter will work out what data type you are using from the context, and use of quotes.