Article image Introduction to Javascript: variables, data types, operators: Variables in Javascript

19.2. Introduction to Javascript: variables, data types, operators: Variables in Javascript

Page 67 | Listen in audio

19.2. Introduction to Javascript: variables, data types, operators: Variables in Javascript

Javascript is a high-level, dynamic language that is commonly used to make web pages interactive. One of the fundamental characteristics of Javascript is the ability to manipulate variables. Variables are basically containers that store information that can be changed and manipulated over time.

Declaring Variables

To declare a variable in Javascript, we use the keywords var, let or const. The var keyword was the traditional way to declare variables, but was replaced by the let and const keywords in the ES6 Javascript update . The difference between these keywords lies in the mutability and scope of the variables.

var x = 10; // Variable declaration using 'var'
let y = 20; // Variable declaration using 'let'
const z = 30; // Variable declaration using 'const'

Data Types

Javascript is a dynamically typed language, which means we do not need to specify the data type when declaring a variable. Data types can be divided into two categories: primitives and objects.

Primitive data types include: Number, String, Boolean, Undefined, Null and Symbol. Objects are a collection of properties, with each property consisting of a key-value pair.

Operators

Javascript provides a variety of operators that can be used to manipulate data. These include arithmetic, comparison, logical, and assignment operators.

Arithmetic Operators

Arithmetic operators are used to perform mathematical operations. These include addition (+), subtraction (-), multiplication (*), division (/), and modulus (%).

var a = 10;
var b = 20;
console.log(a + b); // 30
console.log(a - b); // -10
console.log(a * b); // 200
console.log(a / b); // 0.5
console.log(a % b); // 10

Comparison Operators

Comparison operators are used to compare two values. These include equal (==), not equal (!=), strict equal (===), strict not equal (!==), greater than (>), less than (<), greater than or equal (>= ), and less than or equal (<=).

var a = 10;
var b = 20;
console.log(a == b); // false
console.log(a != b); // true
console.log(a === b); // false
console.log(a !== b); // true
console.log(a > b); // false
console.log(a < b); // true
console.log(a >= b); // false
console.log(a <= b); // true

Logical Operators

Logical operators are used to test the truth of something. These include AND (&&), OR (||), and NOT (!).

var a = true;
var b = false;
console.log(a && b); // false
console.log(a || b); // true
console.log(!a); // false

Assignment Operators

Assignment operators are used to assign values ​​to variables. These include the assignment operator (=), addition and assignment (+=), subtraction and assignment (-=), multiplication and assignment (*=), division and assignment (/=), and modulo and assignment (%=) .

var a = 10;
a += 20; // a = a + 20
console.log(a); // 30
a -= 10; // a = a - 10
console.log(a); // 20
a *= 2; // a = a * 2
console.log(a); // 40
a /= 4; // a = a / 4
console.log(a); // 10
a %= 3; // a = a % 3
console.log(a); // 1

In summary, variables in Javascript are a fundamental part of the language. They allow programmers to store and manipulate data effectively. Understanding how to declare variables, different data types, and how to use operators is crucial to becoming an efficient Javascript developer.

Now answer the exercise about the content:

What are the keywords used to declare variables in Javascript?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Introduction to Javascript: variables, data types, operators: Data Types in Javascript

Next page of the Free Ebook:

68Introduction to Javascript: variables, data types, operators: Data Types in Javascript

4 minutes

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text