DEV Community

Cover image for Getting started with JavaScript - Chapter 2 🚀
devlazar
devlazar

Posted on

Getting started with JavaScript - Chapter 2 🚀

Table Of Contents
* 🤓INTRODUCTION
* 🔢NUMBERS IN JAVASCRIPT
* 🅰STRINGS
* ✅BOOLEAN VALUES
* 💡LOGICAL OPERATOR
* ❓TERNARY OPERATOR
* EMPTY VALUES
* 🦾AUTOMATIC TYPE CONVERSION
* 📝SUMMARY
* ➕APPENDIX 1
* 🙏THANK YOU

🤓 INTRODUCTION

**Welcome, my dear coders! I hope you are all having a codelicious day! Here we are, our second chapter of the Getting started with javascript series. Yesterday, we went through the basic idea of the JavaScript programming language. Today we are going to dive a bit deeper, and maybe tackle the topics mentioned yesterday a bit more.

Let's with the basics, how JavaScript thinks of numbers.

asdf

🔢 NUMBERS IN JAVASCRIPT

Values of the number are numeric values (like we didn't know that 😂). When you type up the number in your program, it will cause the bit pattern for that specific number to come into existence inside the computer's memory.

JavaScript uses a fixed number of bits, 64 of them, to store a single number value. There are only so many patterns you can make with 64 bits, which means that the number of different numbers that can be represented is limited.

➗ THE MATHS BEHIND IT

If you had N decimal digits, you can represent ten to the power of N numbers - 10N; Similarly, given 64 binary digits, you can represent 264 numbers, which is about 18 quintillion or 18000000000000000000 numbers. A lot of numbers! With today's pocket computers (phones) you can easily use 64-bit chunks, and you need to worry about the overflow only when dealing with truly astronomical numbers. But, not all whole numbers less than 18 quintillions fit in a JavaScript number. Those bits also store negative numbers, so one bit indicates the sign of the number. A bigger issue is that nonwhole numbers must also be represented. To do this, some of the bits are used to store the position of the decimal point.

So, the actual maximum whole number that can be stored is more in the range of the 9 quadrillions (15 zeros) - which is pretty huge!

Calculations with whole numbers (also called integers) smaller than the aforementioned 9 quadrillions are guaranteed to always be precise. Unfortunately, calculations with fractional numbers are generally not.

🧮 ARITHMETIC

The main thing to do with numbers is arithmetic. Arithmetic operations, like addition or multiplication, take two number values and produce a new number from them. For example:

11 + 213 * 11

The + and * symbols are called operators. The first stands for addition and the second stands for multiplication. Putting an operator between two values will apply it to those number values and will produce a new number value. The multiplication like in a real-world maths comes first! The subtraction comes with a - sign.

If operators appear together without parentheses, the order in which they are applied is determined by the precedence of the operators.

The * and / have the same precedence. Likewise for + and - operators. If there are multiple operators with the same precedence, they are applied left to right.

There is one more important arithmetic operation. The remainder operation, denoted with a symbol %. X%Y is the remainder of dividing X by Y.

🅰 STRINGS IN JAVASCRIPT

As we mentioned yesterday, strings are used to represent text. They are written by enclosing their content in quotes:

var double_quotes = "Coding is fun!";
var single_quote = 'Coding is fun!';
var backticks = `Coding is fun!`;
Enter fullscreen mode Exit fullscreen mode

Almost anything can be put between quotes, JavaScript will make a string value out of it. But a few characters are more difficult. For example, putting a new line (the characters you get when you press ENTER), can be only included without escaping only when the string is quoted with backticks.

Let's see an example:

var newline_string = 'Coding \n is fun!';
console.log(newline_string)
/*
OUTPUT:
Coding 
is fun!*/
Enter fullscreen mode Exit fullscreen mode

There are situations where you want a backslash in a string to be just a backslash, not a special code. If two backslashes follow each other, they will collapse together.
Example:

var collapse = "Coding \"\\n\" is fun."
console.log(collapse)
/*
OUTPUT:
Coding "\n" is fun.
*/
Enter fullscreen mode Exit fullscreen mode

Strings cannot be divided, multiplied, or substructed, but the + operator can be used on them to concatenate them.
Example:

var concat = "Coding" + "is" + "fun!";
console.log(concat);
/*
OUTPUT:
Codingisfun!
*/
Enter fullscreen mode Exit fullscreen mode

✅ BOOLEAN VALUES

It is often useful to have a value that distinguishes between only two possibilities, like "yes" and "no", or "on" and "off, or "true" and "false". For this purpose, JavaScript has a Boolean type, which has just two values, true and false (this is not a string this is a special type, no double quotes, single quote, or backticks are needed)

var boolean_value = true; //this is a boolean value
var string_value = "true"; //this is a string value
Enter fullscreen mode Exit fullscreen mode

Let's use it in a real example.

console.log(3 > 2); //output: true
console.log(3 < 2); //output: false
Enter fullscreen mode Exit fullscreen mode

💡 LOGICAL OPERATORS

There are also some operations that can be applied to Boolean values themselves. JavaScript supports three logical operators: AND, OR, NOT.
Examples:

console.log(true && false); // output: false
console.log(true && true); // output: true
console.log(true || false); // output: true
console.log(true || true); //output: true
console.log(false || false); //output: false
Enter fullscreen mode Exit fullscreen mode

❓ TERNARY OPERATOR

A conditional operator (or sometimes called the ternary operator since it is the only such operator in the language). The value on the left of the question mark "picks" which of the other two values will come out. When it is true, it chooses the middle value, and when it is false, it chooses the value on the right.

console.log(true ? 1 : 2); // output: 1
console.log(false ? 1 : 2); // output: 2
Enter fullscreen mode Exit fullscreen mode

EMPTY VALUES

There are two special values, written null and undefined, that are used to denote the absence of a meaningful value. They are themselves values, but they carry no information.

Many operations in the language that don’t produce a meaningful value yield undefined simply because they have to yield some value.

🦾 AUTOMATIC TYPE CONVERSION

JavaScript goes out of its way to accept almost any program you give it, even programs that do odd things.
For example:

console.log(10 * null); //output: 0
console.log("5" - 1); //output: 4
console.log("5" + 1); //output: 51
console.log("five" * 2); //NaN
console.log(false == 0); //true
Enter fullscreen mode Exit fullscreen mode

When an operator is applied to the “wrong” type of value, JavaScript will quietly convert that value to the type it needs, using a set of rules that often aren’t what you want or expect. This is called type coercion. The null in the first expression becomes 0, and the "5" in the second expression becomes 5 (from string to number). Yet in the third expression, + tries string concatenation before numeric addition, so the 1 is converted to "1" (from number to string).

📝 SUMMARY

  • Values of the number are numeric values
  • JavaScript uses a fixed number of bits, 64 of them, to store a single number value.
  • If you had N decimal digits, you can represent ten to the power of N numbers - 10N
  • Not all whole numbers less than 18 quintillions fit in a JavaScript number. Those bits also store negative numbers, so one bit indicates the sign of the number. A bigger issue is that nonwhole numbers must also be represented. To do this, some of the bits are used to store the position of the decimal point.
  • Putting an operator between two values will apply it to those number values and will produce a new number value.
  • If operators appear together without parentheses, the order in which they are applied is determined by the precedence of the operators.
  • Strings cannot be divided, multiplied, or substructed, but the + operator can be used on them to concatenate them
  • JavaScript supports three logical operators: AND, OR, NOT
  • Many operations in the language that don’t produce a meaningful value yield undefined simply because they have to yield some value.

➕APPENDIX 1 - LOGICAL OPERATORS VISUAL REPRESENTATION

Alt Text

From an image we conclude:

  • OR is true whenever is X or Y true
  • Exclusive OR is true only when X is true or the Y is true
  • AND is true only when X is true and Y is also true

🙏 THANK YOU FOR READING!

References:
School notes...
School books...

Please leave the comment, tell me about you, about your work, comment your thoughts, connect with me!

☕ SUPPORT ME AND KEEP ME FOCUSED!
Buy Me a Coffee at ko-fi.com

Have a nice time hacking! 😊

Top comments (1)

Collapse
 
devlazar profile image
devlazar

Thank you, Kunal! 🚀