promisepreston
10/12/2019 - 11:23 AM

JavaScript Introduction

JavaScript Introduction

For a long time JavaScript was only used in browsers to build interactive web pages.
Due to large investments by large companies, you can now build full blown web or 

JavaScript was originally designed to run only in web browsers. So every browser has
a JavaScript Engine that can execute JavaScript code. Foe example, the JavaScript engines in 
Firefox - Spidermonkey
Chrome - V8

Node is a JavaScript engine that includes Chrome's V8 engine embedded inside a C++ program.
So we can run JavaScript code outside of a browser, so we pass our JavaScript code to Node for execution.
And this means that with JavaScript we can build the back-end for our web and mobile applications.

In a nutsheell, JavaScript programs can be run inside of a browser or in Node.
Browsers and Node provide a runtime environment for our JavaScript code.

ECMA Script is just a specification, JavaScript is the programming language that confirms to this specification.
So we have this organizaton called ECMA, which is responsible for defining standards, they take care of this ECMA Script specification.

Every browser has a JavaScript engne and we can easily write JavaScript code there without any additional tool.
So open up Chrome, right click on an empty area and go to Inspect.
Now this opens up Chrome Developer Toold. Here, select the Console tab, 
which is where we can write any valid JavaScript code for execution in the browser
  console.log('Hello World');
  alert('yo')
  2 + 2

Now technically, you don't necessarily need Node to execute JavaScript,
because you can execute JavaScript code inside in a Browser or in Node.
But it's good to have Node on your machine, because you use it to install 
third party libraries.

When writing JavaScript in web files, the best practice is to write the JavaScript scripts at the bottom of the body tag,
altough there are exceptions where we write the scripts in the head tag
<body>
  <script>
    console.log('Hello');
  </script>
</body>

You can seperate your JavaScript files into a seperate file, say, index.js, and then referece at the bottom of the bosy tag of the main file.
<body>
  <script src="index.js"></script>
</body>

Executing a code using Node can be donw by using your terminal to navigate to the folder where the file is saved,
and then running the command "node" followed by the name of th file:
  node index.js

Data Types
1. Primitive/Value Types
a. String
b. Number
c. Boolean
d. undefined
e. null

Dynamic Typing
JavaScript unlike other programming languages don't have two kinds of numbers.
It does not have floating point numbers and integers. All numbers are of type number.
  typeof name

2. Reference Types
a. Object - More like a Hash (key-value pairs)
b. Array - The length of an array and the types of objects we have in an array is dynamic.
c. Function
Parameters are what we have at the time of declaration
Arguments are the actual value we supply for that parameter

Operators 
1. Arithmetic
2. Assignment
3. Comparison
4. Logical - The result of a logical expression is not necessarily a true or false, but on the value of the operands that we have.
5. Bitwise

Conditionals
1. If and else
2. Switch case

Loops
1. For
2. While
3. Do...while
4. For...in
5. For...of