[JS] Deep Dive into Javascript 🔥
Tags: javascript, webdev, beginners
Published: 2022-01-24 15:18:25


> Reference
_Codecademy._ Retrieved from https://www.codecademy.com/

***

JavaScript is an object-oriented programming language that powers the dynamic behavior on most websites. Alongside HTML and CSS, it is a core technology that makes the web run. 

***

## Console

The `console.log()` method is used to log or print messages to the console. It can also be used to print objects and other info.

```javascript
console.log('Hi there!');
// Prints: Hi there!
```

***

## Comments

In JavaScript, single-line comments are created with two consecutive forward slashes `//`.

```javascript
// I'm a Single Line Comment
```
And multi-line comments are created by surrounding the lines with `/*` at the beginning and `*/` at the end. Comments are good ways for a variety of reasons like explaining a code block or indicating some hints, etc.
```javascript
/*  
I'm
Multiple
Line
Comment
*/
```


***

## Data Types

_Data types_ are the classifications we give to the different kinds of data that we use in programming. In JavaScript, there are seven fundamental data types:

- Number: Any number, including numbers with decimals (the set of all integers and floating point numbers) : `4`, `8`, `1516`, `23.42`.

```javascript
let amount = 6;
let price = 4.99;
```

* _String_:  They are any grouping of characters \(letters, spaces, numbers, or symbols\) surrounded by single quotes `'...'` or double quotes `"..."`.

```javascript
let single = 'Wheres my bandit hat?';
let double = "Wheres my bandit hat?";
```

* _Boolean_: This data type only has two possible values— either `true` or `false` \(without quotes\). It’s helpful to think of booleans as on and off switches or as the answers to a “yes” or “no” question.

```javascript
let lateToWork = true;
```

* _Null_: This data type represents the intentional absence of a value, and is represented by the keyword `null` \(without quotes\).

```javascript
let x = null;
```

* _Undefined_: This data type is lack of defined value. Variables that are declared but not initialized to a value will have the value `undefined`.

```javascript
var a;
console.log(a); 
// Prints: undefined
```

* _Symbol_: A newer feature to the language, symbols are unique identifiers, useful in more complex coding. No need to worry about these for now.

* _Object_: Collections of related data.


***

## Arithmetic Operators

JavaScript supports arithmetic operators for:

* `+` addition
* `-` subtraction
* `*` multiplication
* `/` division
* `%` modulo

```javascript
// Addition
5 + 5
// Subtraction
10 - 5
// Multiplication
5 * 10
// Division
10 / 5
// Modulo
10 % 5
```

***

## String Concatenation

 In JavaScript, multiple strings can be concatenated together using the `+` operator. In the example, multiple strings and variables containing string values have been concatenated. After execution of the code block, the `displayText` variable will contain the concatenated string.

```javascript
let service = 'credit card';
let month = 'May 30th'; 
let displayText = 'Your ' + service  + ' bill is due on ' +  month + '.';
console.log(displayText);
// Prints: Your credit card bill is due on May 30th.
```

***

## Properties

When you introduce a new piece of data into a JavaScript program, the browser saves it as an instance of the data type. Every string instance has a property called `length` that stores the number of characters in that string.

```javascript
let message = 'good nite';
console.log(message.length);
// Prints: 9

console.log('howdy'.length);
// Prints: 5
```

***

## Libraries

Libraries contain methods that can be called by appending the library name with a period `.`, the method name, and a set of parentheses.

```javascript
Math.random();
// ☝️ Math is the library
```

***

## Methods

Methods return information about an object, and are called by appending an instance with a period `.`, the method name, and parentheses.  You can find a list of built-in string methods in the [JavaScript documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/prototype). Developers use documentation as a reference tool. It describes JavaScript’s keywords, methods, and syntax.

 The `Math.random()` function returns a floating-point, random number in the range from 0 \(inclusive\) up to but not including 1.

```javascript
console.log(Math.random());
// Prints: 0 - 0.9
```

 The `Math.floor()` function returns the largest integer less than or equal to the given number.

```javascript
console.log(Math.floor(5.95)); 
// Prints: 5
```