Working with variables 1
Understanding how to perform operations, assignments, and manipulations with variables is essential for creating dynamic and functional applications. In this post, we’ll explore various ways to work with variables in Javascript.
Operations on Variables
- Loose equality comparison (
==
) - compares only values - Strict equality comparison (
===
) - compares both values and type of variables
We can also return opposite boolean value by using !
before =
.
console.log("5" == 5); // true
console.log(null == undefined); // true
console.log("5" === 5); // false
console.log(null === undefined); // false
console.log("a" != "a"); // false (because a is equal to a but we negate with !)
console.log(54 != "54"); // false (because 54 is equal to "54" in value but we negate with !)
console.log(54 !== "54"); // true (because 54 is not equal to "54" neither in value nor in type but we negate with !)
Mathematical Operations
let x = 10;
let y = 5;
let sum = x + y; // Sum of x and y
let difference = x - y; // Difference between x and y
let product = x * y; // Product of x and y
let quotient = x / y; // Quotient of x divided by y
let remainder = x % y; // Remainder of 5 in 10 (which is 0 because 10-5-5=0)
let remainder1 = x % 3; // Remainder of 3 in 10 (which is 1 because 10-3-3-3=1 and we can't put another -3)
let remainder2 = 5 % 30; // Remainder of 30 in 5 (which is 5 because 30 is greater than 5 so we will always return first value if its lower than second value)
let result = a ** b; // a to the power of b
console.log(result); // 4
let isGreater = x > y; // returns boolean x is greater than y
let isLess = x < y; // returns boolean x is less than y
let isGreaterOrEqual = x >= y; // returns boolean x is greater or equal than y
let isLessOrEqual = x <= y; // returns boolean x is less or equal than y
String Concatenation
We can merge or concatenate multiple variables. This can be done with multiple types of variables but there are some interesting results if you concatenate types that can’t really be concatenated(⏭️).
let firstName = "John";
let lastName = "Doe";
let x = 2;
let fullName = firstName + " " + lastName; // Concatenating strings
let concatTypes = firstName + x; // Concatenating strings
console.log(fullName); // John Doe
console.log(concatTypes); // John2
Template Literals
If we use backticks (`) we can insert valid JS code in expressions ${expression}
.
This allows us multi-line strings, string interpolation and a lot of other useful things.
let firstName = "John";
let lastName = "Doe";
let fullName = `My name is ${firstName} ${lastName}`;
console.log(fullName); // My name is John Doe
let multiLine = `My name is
John Doe
`;
console.log(multiLine);
// My name is
// John Doe
let x = 1;
let y = 2;
let result = `Result is: ${x + y}`;
console.log(result); // Result is: 3
Different Operators
Here we have:
- logical AND (
&&
) - both operands(values) need to betrue
for this to returntrue
. - logical OR (
||
) - either operand(value) need to betrue
for this to returntrue
. - logical NOT (
!
) - will return opposite value of boolean, if passedtrue
it will returnfalse
and the other way around. - Nullish coalescing operator (
??
) - returns left value if that value is notnull
orundefined
, otherwise it will return right value.
let isTrue = true;
let isFalse = false;
let resultAnd = isTrue && isFalse; // Logical AND operation
let resultOr = isTrue || isFalse; // Logical OR operation
let notTrue = !isTrue; // false
let notNotTrue = !!isTrue; // true
const a = 5 ?? 3;
console.log(a); // 5
const b = false ?? 3;
console.log(b); // false
const c = null ?? 3;
console.log(c); // 3
There are also right shift (>>
) and left shift(<<
) operators but those are a bit more complex and will be covered in intermediate posts(⏭️).
Assignments and Manipulations
Assignment Operators
NodeJS provides various assignment operators to assign values to variables.
+=
, -=
, /=
and *=
are called compound assignments.
let a = 2;
let b = 2;
a += b; // Equivalent to: a = a + b
a -= b; // Equivalent to: a = a - b
a /= b; // Equivalent to: a = a / b
a *= b; // Equivalent to: a = a * b
Increment and Decrement
We have:
- Increment (
++
) - will increase the number - Decrement (
--
) - will decrease the number
If used postfix, with operator after operand (for example, x++
), the increment operator increments and returns the value before incrementing.
If used prefix, with operator before operand (for example, ++x
), the increment operator increments and returns the value after incrementing.
let count = 0;
let x = 0;
x++; // Equivalent to: x = x + 1
console.log(count++); // will return 0 because we put ++ after but the actual count value is 1
console.log(count); // 1
console.log(count--); // will return 1 because we put -- after but the actual count value is 0
console.log(count); // 0
console.log(++count); // will return 1 because we put ++ before and the actual count value is 1
console.log(count); // 1
console.log(--count); // will return 0 because we put -- before and the actual count value is 0
console.log(count); // 0
Manipulating Strings
let message = "Hello";
message += " " + "NodeJS"; // Appending strings
console.log(message); // Hello NodeJS
// or
let newMessage = "Hello";
newMessage += ` NodeJS`;
console.log(newMessage); // Hello NodeJS
Don’t let message += " " +
confuse you, we are just adding a space between Hello
and NodeJS
.
What now
Soon you will learn how to connect everything into a meaningful system but you need to be prepared so try everything you learned today.
In case you forgot, just create a new file index.js
and write the code inside and then run it with node index.js
. Here is the full post.