Skip to content

sidram403/JavascriptFromScratch

Repository files navigation

JavaScript From Zero to Hero

Before diving into the code, it's helpful to understand what JavaScript is and what it can be used for.

Definition

JavaScript is a programming language that is primarily used for creating dynamic and interactive web pages.

Where JavaScript we use

  • It is often used in conjunction with HTML and CSS to make websites more engaging and interactive.

    Example : Some examples of JavaScript-powered features on websites include dropdown menus, pop-up windows, and form validation.

Now, let's move on to actually writing some JavaScript code.

Here are some basic concepts you should learn when starting out:

1. Variables

  • Variables are used to store data in JavaScript.
  • You can declare a variable using the var, let, or const keywords, depending on whether you want to allow the variable to be reassigned later or not.
  • For example:
var myVariable = "Hello, world!";
let myOtherVariable = 42;
const myConstant = true;

2. Data Types

  • JavaScript has several different data types, including strings, numbers, booleans, and objects.
  • You can use the typeof operator to check the type of a variable.
  • For example:
var myString = "Hello, world!";
var myNumber = 42;
var myBoolean = true;
var myObject = {name: "John", age: 30};

console.log(typeof myString); // Output: string
console.log(typeof myNumber); // Output: number
console.log(typeof myBoolean); // Output: boolean
console.log(typeof myObject); // Output: object

3. Functions

  • Functions are used to perform a specific task in JavaScript.
  • They can take parameters as input and return a value.
  • Here's an example:
function addNumbers(num1, num2) {
  return num1 + num2;
}

console.log(addNumbers(2, 3)); // Output: 5

4. Conditional Statements

  • Conditional statements allow you to execute different code depending on whether a condition is true or false.
  • The if statement is the most basic conditional statement in JavaScript.
  • Here's an example:
var age = 18;

if (age >= 18) {
  console.log("You are an adult.");
} else {
  console.log("You are not an adult yet.");
}

5. Loops

  • Loops are used to repeat a block of code multiple times.
  • The for loop is a common type of loop in JavaScript.
  • Here's an example:
for (var i = 0; i < 5; i++) {
  console.log("The value of i is " + i); // Output: 0,1,2,3,4
}

This should give you a good starting point for learning JavaScript. There are many other concepts and features to explore, but mastering these basics will give you a strong foundation.

Here are some advanced concepts you should learn

6. Arrays

  • Arrays are used to store a collection of values in a single variable.
  • You can create an array using square brackets and add elements to it using commas.
  • Here's an example:
var myArray = [1, 2, 3, 4, 5];

console.log(myArray[0]); // Output: 1
console.log(myArray.length); // Output: 5

7. Objects

  • Objects are similar to arrays, but they store key-value pairs instead of just values.
  • You can create an object using curly braces and add properties to it using colons.
  • Here's an example:
var myObject = {
  name: "John",
  age: 30,
  isStudent: true
};

console.log(myObject.name); // Output: John
console.log(myObject.age); // Output: 30

8. DOM Manipulation

  • The Document Object Model (DOM) is a programming interface that allows JavaScript to interact with HTML and CSS on a web page.
  • You can use JavaScript to change the content and appearance of a web page by manipulating the DOM.
  • Here's an example:
// HTML code: <p id="myParagraph">Hello, world!</p>

var myElement = document.getElementById("myParagraph");
myElement.innerHTML = "Goodbye, world!";

9. Events

  • Events are actions that happen on a web page, such as a user clicking a button or scrolling the page.
  • You can use JavaScript to detect and respond to events by adding event listeners to elements on the page.
  • Here's an example:
// HTML code: <button id="myButton">Click me!</button>

var myButton = document.getElementById("myButton");
myButton.addEventListener("click", function() {
  console.log("Button clicked!");
});

10. Error Handling

  • Error handling is an important part of programming in any language, and JavaScript is no exception.
  • You can use try-catch blocks to handle errors in your code and prevent them from crashing your program.
  • Here's an example:
try {
  var myVariable = undefinedVariable;
} catch (error) {
  console.log("Error: " + error.message);
}

These are just a few more concepts to add to your JavaScript toolkit. As you continue learning and practicing, you'll discover many more features and techniques that can help you create powerful and interactive web pages. Good luck on your JavaScript journey!

Contents in detail

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published