Javascript basics
/ 5 min read
Updated:Table of Contents
Introduction to JavaScript
Computers understand 0’s and 1s but writing programs in 0s & 1s is tough, so Programming Languages like C++, C, JavaScript were introduced for easier programming. Note : In the end, programming languages are translated into 0s 1s.
Programming languages like FORTRAN, BASIC etc. do exist, but only for computers, not for browsers. Thus, emerged the Language of browser – JavaScript.
ECMA International (formally European Computer Manufacturers Association) is a non-profit organization that develops standards in computer hardware, communications, and programming languages. Hence, the ECMAScript : Guidelines/Standards for building JavaScript language or Compiler.
List of JS Engines/Compilers
- Google Chrome – V8
- Mozilla – SpiderMonkey
- Safari – JavaScriptCore
Running JS on computer
- Node.js is JS
Framework,Language, Runtime, – Uses V8 Engine of Chrome. - **Deno.land** , another TS and JS runtime built on V8. Uses Rust as its core language. It was created by Ryan Dahl, the ****creator of Node.js, to fix a long list of inherent problems in Node.js.
- Bun, another JS runtime, instead of V8 it uses JavaScriptCore of Apple Safari, core written in Zig Language.
JavaScript Basics
- Variables : of which value can change
- Loops : for, while to iterate over arrays, objects.
// Variables with different datatypes
var userName = "Harkirat" // stringvar coursePrice = 5999 // numbervar purchased = true // booleanvar completeUserData = { // Object(key-value pairs) name : "Harkirat" coursePrice : 5999 purchased : true}var users = [ "Rookie", "Harkirat" ] // array
- Functions : Helps to organize the code and achieve DRY (Don’t Repeat Yourself).
- Callback : When we can pass a function as an argument into another function, usually the last argument. Why ? Helps to achieve below 👇
// From thisfunction doArithmetic(firstEl, secondEl, whatToDo) { if (whatToDo === "sum") { var answer = sum(firstEl, secondEl); return answer; }
if (whatToDo === "multiply") { var answer = multiply(firstEl, secondEl); return answer; }}
// To thisfunction doArithmetic(firstEl, secondEl, funToRun) { return funToRun(firstEl, secondEl);}
Core Concepts
JavaScript runs on browsers and using Node.js, also in backend servers.
Single Threaded : A processor has multiple cores, and a core is where code runs. Think of cores as number of people, higher the number, more tasks can be completed at once. In JS, despite the more number of cores, say 10, JS runs on a single core only, hence single threaded. Simply putting, parallel computing is not possible. 😲 This is why code runs sequentially. One after One.
Synchronous : Synchronous tasks go in sync like one by one, where each step of the task must complete before the next step can begin. Think of it like people in queue waiting to buy tickets, the next person in line can only buy their ticket once the person ahead of them has completed their purchase. Therefore, a line of code must be finish execution before it moves to the next. Hence, “Blocking” Nature.
Asynchronous : Asynchronous tasks (Background Tasks) take time, how much we don’t know. Think of it like parcel will be delivered by today, but not sure what time. CBSE will releaser result today, but not sure what time. Similarly, when we send a http request, a response will be received but don’t know at what time. That is what needs to be run in the background, which is possible via Asynchronous Programming, this allows the program to continue executing other operations or tasks while waiting for the completion of the asynchronous task. . Thus, “Non-Blocking” behaviour enables greater concurrency.
Asynchronous Tasks
- Keyboard Input in game
- Network Requests
- File Operations
- Database Queries
Terminology
Loosely/Weakly Typed : If I can input any data type say string, number, boolean to variable or a parameter passing to a function, then it is loosely typed. Example : JavaScript.
Strongly typed : When I can input only a specific data type defined by programmer to a variable or a parameter passing to a function, also what type of data the function should return, then it is strongly typed. Example : C++
Loosely typed example in JavaScript:
// Can put "any type of data" in variable "iAmVariable"var iAmVariable = "Harkirat";iAmVariable = true;iAmVariable = 5999;
Strongly typed example in C++:
// Can put "only specific type of data" in variable "iAmVariable"int iAmVariable = 5999;iAmVariable = "Harkirat" // ERROR !!! NOT POSSIBLE, only number
Statically typed languages check the data types and look for type errors during compile time. Ex : C++, Java
Dynamically typed languages check the data types and look for type errors during runtime. Ex : JS