JavaScript Basic Operators - JavaScript: The Ultimate Crash Course Learning JavaScript within a Day with New Approach for Faster Programming (Save Time and Effort) (2016)

JavaScript: The Ultimate Crash Course Learning JavaScript within a Day with New Approach for Faster Programming (Save Time and Effort) (2016)

Chapter 2: JavaScript Basic Operators

JavaScript basic operators are used to manipulate different values in your programming. JavaScript uses an assignment operator, an equal sign in order to assign values to a variable. An illustration is as below:

var a = 8;

var b = 9;

Arithmetic operators on the other hand are used to compute values. These are given the signs + - * /. Below is an example:

(8 + 9) * 17

These includes division and multiplication, for example:

5 * 6;

5 / 6;

And decrementing and incrementing:

var x = 6;

var y = ++x; // pre-increment: y equals 5; x equals 6

var z = x++; // post-increment: z equals 5; x equals 6

Concatenation will also be useful. Here is an example:

var foo = 'hello';

var bar = 'world';

console.log(foo + ' ' + bar); // 'hello world'

Numbers and Strings Operations

In JavaScript, the behavior of strings and numbers is often very different manner when compared to how they behave with other programming languages. It is therefore important to understand each step carefully to be able to operate them with ease when you start programming:

Addition vs. concatenation

Here is an example of how additions and concatenations happen on numbers and strings:

var foo = 5;

var bar = '6';

console.log(foo + bar); // 56. uh oh

How to force a string to behave like numbers:

var foo = 5;

var bar = '6';

// coerce strings towards a number

console.log(foo + Number(bar));

When a number constructor is identified as a function as it is illustrated above, it will have the ability to cast its argument to numerical form. The unary plus operator carries out a similar function, therefore it can also be used. Here is an illustration:

console.log(foo + +bar);

Logical operators

The logical operators in JavaScript make it possible to gauge a sequence of operations using AND as well as OR operations. Look at the illustration below:

var foo = 5;

var bar = 0;

var baz = 6;

foo || bar; // takes back 5, that is true

bar || foo; // takes back 5, that is true

foo && bar; // takes back 0, that is false

foo && baz; // takes back 6, that is true

baz && foo; // takes back 5, that is true

From the illustration above:

- The operator // brings back the value of the initial operation. Should it occur that none of the operations is true, the last of the two operations will be returned.

- The && operator brings the value of the primary false operation or that of the final operation if both of the operations were true.

Sometimes logical operations are used for the flow control rather than making use of if statements by some developers. See the example below:

// do a thing with foo if foo is true

foo && do something(foo);

// set bar to bax if bax is true;

// or else, set it to the take back

// value of==for createBar()

var bar = bax || createBar();

The above is a different kind of style, that is elegant and quite pleasant but it is not easy to read, particularly for those without experience. Only after advancing the skills in JavaScript can a beginner be able to read that.

Note: you need to know which kinds of values are truth and which ones are false so as to use flow control effectively all the time. There are times when values that look like they should evaluate in one way evaluate in a different way instead. Values that evaluate to true are illustrated as:


'any string';

[]; // an empty array

{}; // an empty object

1; // any non-zero number

And those that evaluate to the false are illustrated as:


''; // an empty string

NaN; // JavaScript's "not-a-number" variable


undefined; // be careful -- undefined can be redefined!

The conditional code

This is only applicable when you want to run a large amount of code as long as certain conditions apply. Here, you will use Flow control through if and else blocks. Here is an example in a flow control:

var foo = right;

var bar = wrong;

if (bar) {

// this code shall never run



if (bar) {

// this code shan't run

} or {

if (foo) {

// this code will run

} else {

// this code would run if foo and bar were both wrong



Always remember that use of curly braces will help you in the creation of more readable codes, even when they are not really necessary with single-line if statements. You can therefore use them as much as you want for better results. Also, avoid defining functions using the a name which is the same many times within different if/else blocks. This may prevent you from achieving the desired results.