19.1. Introduction to Javascript: variables, data types, operators
Javascript is a dynamic and interpreted programming language that is essential for creating interactive web pages. It is one of the three main technologies that make up the World Wide Web, along with HTML and CSS. In this section, we will introduce some of the fundamental concepts of Javascript: variables, data types and operators.
Variables
In Javascript, a variable is a container for storing data values. To declare (create) a variable, we use the 'var' keyword, followed by the variable name. For example, var x;
. In this example, 'x' is the variable. Now, we can store a value in this variable using the '=' assignment operator. For example, x = 5;
. Now, the variable 'x' contains the value 5.
Variables in Javascript can contain many different types of data: numbers, strings, objects, and more. This brings us to the next topic: data types.
Data Types
Javascript has a variety of data types. Here are the most common ones:
- Numbers: These can be integers or decimals. For example,
var x = 10;
orvar y = 3.14;
. - Strings: These are sequences of characters, usually enclosed in quotation marks. For example,
var greeting = "Hello, world!";
. - Boolean: This is a logical data type that can have only one of two values: true (true) or false (false). For example,
var isRaining = false;
. - Objects: These are collections of named values, similar to 'dictionaries' in some other programming languages. For example,
var person = {firstName: "John", lastName: "Donate"};
. - Undefined: This is a special data type that represents an undefined value. A variable that has been declared but not assigned a value has the type 'undefined'.
- Null: This is another special data type that represents a null or "nothing" value.
These data types allow Javascript programmers to manipulate a variety of information in their programs.
Operators
Operators are symbols that perform operations on values. Javascript has many types of operators, including:
- Arithmetic operators: These perform mathematical operations. For example,
x + y
(addition),x - y
(subtraction),x * y
(multiplication),x / y
(division), andx % y
(module, or remainder of the division). - Assignment operators: These assign values to variables. The simplest is the '=' operator. For example,
x = 5;
. There are also compound assignment operators that perform an arithmetic operation and an assignment at the same time. For example,x += 5;
is the same asx = x + 5;
. - Comparison operators: These compare two values and return a boolean (true or false). For example,
x == y
(equal to),x != y
(not equal to),x > y
(greater than),x < y
(less than),x >= y
(greater than or equal to), andx <= y
(less than or equal to) . - Logical operators: These operate on Booleans. For example,
x && y
(logical AND),x || y
(logical OR), and!x
(NOT logical).
Operators allow Javascript programmers to perform complex operations and make decisions in their programs.
In summary, variables, data types, and operators are fundamental Javascript concepts that every front-end developer must understand. Understanding these concepts is the first step to becoming an effective Javascript developer.