Basics of web development

JavaScript Operators

Easy
20 min

Variable values are often manipulated using various operators and functions. Below are some commonly used operators and functions:

Arithmetic operators

Used for mathematical calculations such as addition and subtraction, multiplication and division. For example, you can calculate the sum of two variables as follows:

var x = 5;
var y = 10;
var sum = x + y;

This calculates the sum of variables "x" and "y" and stores the result in a new variable named "sum".


Comparison operators

Used to compare two variables and give a true or false value depending on whether they are equal or smaller. For example, you can compare two variables as follows:

var x = 5;
var y = 10;
var comparison = x < y;

This compares whether the variable "x" is smaller than the variable "y", and stores the result in a new variable called "comparison".


String Concatenation

You can combine two strings using the plus operator. For example, you can combine two strings as follows:

var firstname = "Matt";
var lastname = "Meikäläinen";
var name = firstname + " " + lastname;

This connects two strings ("Matti" and "Meikäläinen") and adds a space between them. The result is saved in a new variable named "nimi".

Example

Here is an example of JavaScript code that uses various operators:

Practice

Kokeile itse!

Valitettavasti Replit-palvelu on muuttnut lennosta eikä enää anna suorittaa näitä koodeja suoraan selaimessa. Voit klikata alla olevaa "Open in Replit" linkkiä ja avata koodin Replit-palvelussa. Meillä on työn alla etsiä Replitille korvaaja.

hakatemia pro

Ready to become an ethical hacker?
Start today.

As a member of Hakatemia you get unlimited access to Hakatemia modules, exercises and tools, and you get access to the Hakatemia Discord channel where you can ask for help from both instructors and other Hakatemia members.