19. Introduction to JavaScript: Variables, Data Types, Operators
JavaScript is a dynamic programming language that is primarily used to add interactivity to web pages. It allows developers to manipulate web page elements and change their behavior according to user actions. In this chapter, we will explore the basic concepts of JavaScript, such as variables, data types, and operators.
Variables
Variables are used to store data that can be used and modified later in your code. In JavaScript, you can declare a variable using the 'var', 'let' or 'const' keyword. For example:
var name = 'John';
let age = 25;
const pi = 3.14;
Here, 'name' and 'age' are variables that store a string and a number, respectively. 'pi' is a constant that stores the value of pi. The main difference between 'var', 'let' and 'const' is scope and reassignment. 'var' has a function scope, while 'let' and 'const' have a block scope. Furthermore, 'const' does not allow reassignment of values.
Data Types
JavaScript has six primitive data types:
- String: represents a sequence of characters. For example: 'Hello, World!'
- Number: represents a numeric value. For example: 10, 3.14
- Boolean: represents a true or false value.
- Undefined: represents an undefined value.
- Null: represents a null value.
- Symbol: represents a unique value that is not equal to any other value.
In addition to these, JavaScript also has a data type called Object to store collections of data and more complex entities.
Operators
Operators are used to perform operations between variables and values. JavaScript has several types of operators:
- Arithmetic operators: are used to perform mathematical operations. For example: +, -, *, /, %, ++, --
- Assignment operators: are used to assign values to variables. For example: =, +=, -=, *=, /=
- Comparison operators: are used to compare two values. For example: ==, !=, ===, !==, >, <, >=, <=
- Logical operators: are used to combine conditions. For example: &&, ||, !
These are the basic JavaScript concepts you need to understand to start programming in JavaScript. In the next chapter, we will explore more about functions and how they are used in JavaScript.