DEV Community

Cat4eve
Cat4eve

Posted on

JS#2, JavaScript functions

Made by Karapet Khachatryan, AUA CS freshman student.

The second topic of this blog course would be about functions in JS. First part could be found here https://dev.to/cat4er/js1-javascript-variable-differences-18i.

During previous course we have discussed variables and their types. To explain functions, it is important to understand the oprations. There exist serveral group of opearations we will explore.

1) Basic math operations ( +, -, *, / )

In JS it is very easy to make general operations we do in math. Addition, subtraction, multiplication and division are represented with unique signs. In order to apply operation, the symbol must be placed in between of two numbers or variables containing numbers.

Hint, if one of the operations would not be number we are risking to get NaN (Not a Number) as an answer.

var a = 10;
var b = 2;
var c = a + b; // result is 12 stored in variable c
var d = a - b; // result is 8 stored in variable d
var e = a * b; // result is 20 stored in variable e
var f = a / b; // result is 5 stored in variable f
Enter fullscreen mode Exit fullscreen mode

2) Logical operations (and, or, not)

The unique feature of all programming languages is that they are Turing completeness. That means that those languages contains logical operators, enough to make any logical expression. For example, A and not B and not C or A. In JS we have && (and), || (or), ! (not) operators. Likewise numbers, they must be placed in between two variables or operands. However, during calculation any variable would be automatically translated into boolean language (true or false). It would be beneficial to know general rules about translation to boolean language. All inserted numbers would be equal to true, excpet 0, which is false for JS. All type of string would be equivalent to true, except '' (empty string).

var a = true;
var b = false;
var c = true;

var d = !a; // result is false stored in variable d
var e = b || c; // result is true stored in variable e
var f = (a && b) || !c; // result is false stored in variable f
Enter fullscreen mode Exit fullscreen mode

3) Comparison operators (==, >, <)

==, <, > are comparison operators, which compares two operands (numbers or booleans).

var a = 20;
var b = 18;
var c = 20;

var d = a <= c; // result is true stored in variable d
var e = b > a; // result is false stored in variable e
var f = c > a; // result is false stored in variable f
Enter fullscreen mode Exit fullscreen mode

4) Conditional operations (if, else)

In JS we can make possibilities and cases and make everything happen under some conditions.

var age = 20;
var undereighteen;
if (age > 18) {
   undereighteen= false;
} else {
    undereighteen = true;
}
Enter fullscreen mode Exit fullscreen mode

We know how to make basic operations with JS, right? Let's talk about functions!

Function in fact is a piece of code which we can use multiple time. Let's say I want to make a multiplication of two numbers, but we can only add two numbers (we can not use multiplication operation). Imagine a particular example, when we want to calculate the multiplication 2 and 3, which is 6 as we know, but our programm would be able to calculate it. We know that 2 * 3 is the same as 2 + 2 + 2 or 3 + 3. In generall if we have some x, y variables, which we want to calculate, all we need is to add x number y times or y number x times. It is time to introduce functions!

function multiply(x, y) {
    return x * y;
}
Enter fullscreen mode Exit fullscreen mode

This is the way we define function. Likewise variables, functions have a key word, by which we define them and name, by which we remember them. Key word is not var or let but the word function itself is a that word, which helps JS to understand that we want to create a function. multiply is the name of our function. Next, likewise functions from math, ours also have some inputs (in this case two inputs - x and y), which are considered as already defined variables. Finally, the braces (curly brackets) are setting a space, inside which our reproducable code would be written. return is a key word by which our function returns us something (in math we usually call this process a map from x, y to z).

Let's try to write an addition for one number.

function multiply(x, y) {
   if (x == 1) {
       return y;
   }
   if (x == 2) {
       return y + y;
   }
   if (x == 3) {
       return y + y + y;
   }
}
Enter fullscreen mode Exit fullscreen mode

To call functions we write its name and general brackets, inside of which we write actual arguemnts.

multiply(2, 3);
Enter fullscreen mode Exit fullscreen mode

Next, we can create a variable and assigne the return value of function to it.

var a = multiply(2, 3); // result will be 6 stored in a
Enter fullscreen mode Exit fullscreen mode

In this course we have learnt the basics of functions and operations, during next course we will improve our knowledge and make better and more efficient code.

Top comments (0)