100xDevs Full Stack Web Development Cohort Journey
100xDevs Full Stack Web Development Cohort Week 1 : Day 1 + Week 1 Overview
We have started learning with the foundation. For this week, the topics covered were
Foundation for Js
Async Nature of Js
Why do we need languages?
Machines won't understand Hindi, English, or Spanish. They don't even understand the code we write. They are only amenable to the binary script, the 010001001110 language.
The code we write is converted to the binary language, and then the machine will understand that. However, it would have been highly inefficient for humans to write binary code. That's why we have programming languages; we write the code in the script we understand, and the code is translated into binary by something called Compilers (Some languages have these, and some don't).
Compiled vs. Interpreted languages
There are many languages, and every language may have different ways of handling the code.
For example, for a C++ code to run, the code first will be converted to a binary code file, and then the code will be run. Meanwhile, if there are any syntactical or other errors in the code, they will be thrown at the time of compiling. Making it more secure but a bit tedious and strict for beginners to learn.
Making C++ a compiled language
On the other hand, for a JS code to run, the code doesn't need to be converted to binary before running it. The interpreter would go line by line, converting the code to binary and making the machine understand it. The errors here are thrown (if any), at the time of execution. Js is an interpreted language.
Single-threaded nature of JS
Humans are well trained for multi-tasking, but not the human brain. The human brain can process/do one thing at a time, explaining the single-threaded nature of the human brain. Before diving into this, let's deal with the jargon, core, and threads.
A core is a small CPU or processor built into a big CPU or CPU socket. It can independently perform or process all computational tasks. It is like a worker would compute the code.
A thread typically refers to a sequence of software code the computer and its CPU must execute. It is like a conveyor belt, where you pass the instructions to the core.
Single-threaded nature: programming languages use cores for processing the instructions and code lines. Your machine may have multiple cores, but JS would use only one. Even though the code may lag and the core may burn out.
Context-switching: JavaScript's context-switching occurs as tasks shift, managing asynchronous operations through callbacks and the event loop. This single-threaded language ensures non-blocking execution by leveraging mechanisms like promises and async/await.
// Asynchronous operation using setTimeout console.log('Start'); setTimeout(() => { console.log('Async Operation'); }, 1000); console.log('End'); // Output: Start, End, Async Operation (after 1 second)
Cluster module: The Cluster module in Node.js allows the creation of a cluster of worker processes, each running on a separate core. The master process manages worker creation, and incoming network connections are distributed among workers for load balancing. This enhances performance by utilizing multiple CPU cores, overcoming the limitations of the single-threaded event loop.
// Example using the Cluster module in Node.js const cluster = require('cluster'); const os = require('os'); if (cluster.isMaster) { // Create a worker for each CPU core for (let i = 0; i < os.cpus().length; i++) { cluster.fork(); } } else { // Worker process code // Handle tasks within each worker }
Variables, primitives, and Objects
Variables are containers for storing data values in JavaScript. They are declared using
var
,let
, orconst
. Variables can hold various types of data, including primitives and objects.Primitives are basic data types in JavaScript that are immutable and directly represent a single value. There are six primitive data types:
string
,number
,boolean
,null
,undefined
, andsymbol
. Primitives are passed by value.let name = "John"; // string let age = 25; // number let isActive = true; // boolean let person = null; // null let status; // undefined let uniqueId = Symbol(); // symbol
Objects are complex data types that group key-value pairs and represent a collection of related data and functionality. Objects are mutable and can store various data types, including other objects.
let personObject = { name: "John", age: 25, isActive: true, address: { city: "Example City", zip: "12345", }, sayHello: function() { console.log("Hello!"); }, }; let array = [1,2,3,4,5,6];
Calling Back Function
The callback function is generally the function we pass as an argument to the function.
function MathOperation(a,b,fcn){
return fcn(a,b) // fcn is the call back function parameter
}
function sum(a,b){
return a+b;
}
function subtract(a,b){
return a-b;
}
console.log(MathOperation(1,2,sum)) //sum is the call back function
setTimout() function
setTimout(()=>{ // this is the anonymous call back function
console.log("This message appearing after 5 seconds")
},5000)
// setTimeout() function waits for the given time here it is 5000 ms, and executes the call back function
// This function is run asynchronously
For the week 1 overview, we have completed the JS assignments and solved some interview questions.
Please find the related code and information in the GitHub Repository.
Stay tuned for more blogs on the projects and other algorithms.