1.1 JavaScript Foundation
Greetings! Let's explore why JavaScript is a powerhouse for web development and get our hands dirty with some foundational coding!
Why JavaScript?
1. Interpreted Nature: JavaScript is an interpreted language. Unlike compiled languages that need a separate compilation step, JavaScript runs directly in the browser. This not only simplifies the development process but also aids in easy debugging.
2. Dynamic and Flexible: JavaScript is dynamically typed, which means you don't need to specify the data type of a variable. This flexibility allows for faster development and is particularly advantageous in smaller codebases. Unlike statically typed languages like C++, JavaScript adapts on the fly, making it a versatile choice.
(C++ is better tho, xD)
3. Single-Threaded Nature: While some consider JavaScript's single-threaded nature a limitation for scalable systems, this design choice ensures a smooth user experience, preventing the browser from freezing during execution. (No wait, I personally don't like single threaded Nature)
Now, let's put our newfound knowledge to the test!
Your First JavaScript Code
Open Repl.it and create a new REPL with the Node.js template.
In the
index.js
file, write the classic "Hello, World!" program:console.log("Hello World");
Click on "Run" or navigate to the shell and type
node index.js
to see the output.
JavaScript Fundamentals
Simple Primitives:
Variables (let
, var
, const
): Variables are containers for storing data values. let
and const
are block-scoped, while var
is function-scoped.
let fn = "Rashmin";
let age = 19;
Conditional Statements (if-else
): Conditional statements allow us to make decisions in our code.
if (age >= 18) {
console.log("Hello, adult!");
} else {
console.log("Hey, you're still a minor!");
}
Complex Primitives:
Arrays and Objects: Arrays and objects allow us to organize and store data more efficiently.
const ages = [3, 10, 18, 13, 4, 11, 16];
for (let i = 0; i < ages.length; i++) {
if (ages[i] % 2 === 0) {
console.log(ages[i] + " ");
}
}
NOTE:
We suggest learning JavaScript from YouTube tutorials or documentations. It would be easy if you already know a programming language like C/C++/Java.
https://developer.mozilla.org/en-US/docs/Web/JavaScript
Functions and Callbacks:
Defining Functions: Functions are blocks of code designed to perform a particular task.
function greet(name) {
console.log("Hello, " + name + "!");
}
greet("Rashmin");
Callback Functions: Callback functions are functions passed as arguments to other functions.
function processArray(arr, callback) {
for (let i = 0; i < arr.length; i++) {
callback(arr[i]);
}
}
processArray(ages, function (element) {
console.log("Processing element: " + element);
});
Explore and learn!
1.2 Basic JS APIs rashmin.hashnode.dev/dev-1-2
Home article https://rashmin.hashnode.dev/dev