A gentle introduction to Java Script

A gentle introduction to Java Script

JavaScript is a versatile, high-level programming language that's widely used in web development. It can be used to add interactivity to websites, handle events, manipulate the DOM (Document Object Model), and much more. Here's a gentle introduction to JavaScript.

What is it ?

Java script is a scripting language that is written from top to bottom. It is used to create dynamic content on websites. Unlike HTML and CSS , java script adds behaviors to web pages, making them interactive to the user.

JavaScript in HTML

Java script can be embedded in the HTML using the <script> tag.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JavaScript Introduction</title>
</head>
<body>
    <h1>Hello, World!</h1>
    <button onclick="displayMessage()">Click Me</button>

    <script>
        function displayMessage() {
            alert("Hello from JavaScript!");
        }
    </script>
</body>
</html>
        

When you click on the button you get a message as "Hello from JavaScript" as shown below:

JavaScript Syntax Basics

a) Variables

Variables are used to store data values. You can declare a variable using var, let, or const.

  • var: Globally or function-scoped.
  • let: Block-scoped.
  • const: Block-scoped and cannot be reassigned. It is a constant

let name = "Ram";    // String
const age = 25;       // Number
var isStudent = true; // Boolean        

b) Data Types

JavaScript has several data types:

  • Primitive Types: String, Number, Boolean, null, undefined, Symbol, and BigInt.
  • Objects: Collections of properties (e.g., Arrays, Functions, and Objects).

let student= { firstName: "Ram", lastName: "Doe", age: 25}; // Object
let numbers = [1, 2, 3, 4, 5]; // Array        

c) Operators

JavaScript supports a variety of operators, including:

  • Arithmetic: +, -, *, /, %
  • Assignment: =, +=, -=
  • Comparison: ==, ===, !=, !==, >, <, >=, <=
  • Logical: &&, ||, !

let x = 10;
let y = 5;
let result = x + y; // result is 15        

d) Functions

Functions are reusable blocks of code that perform a specific task.

function greet(name) {
    return "Hello, " + name + "!";
}

let greeting = greet("Ram");
console.log(greeting); // Outputs: Hello, Ram!        

e) Control Structures

JavaScript supports various control structures for decision-making and looping.

  • Conditional Statements: if, else if, else

let age = 18;

if (age < 18) {
    console.log("Minor");
} else if (age === 18) {
    console.log("Just turned adult");
} else {
    console.log("Adult");
}        

DOM Manipulation

JavaScript can be used to manipulate the DOM, allowing dynamic changes to the content and structure of a web page. The below code finds an HTML element with the ID myElement and changes its text content to "New Content".

document.getElementById("myElement").innerText = "New Content";        

Event Handling

JavaScript can respond to user actions like clicks, form submissions, or key presses.

document.getElementById("myButton").addEventListener("click", function() {
    alert("Button clicked!");
});        

Objects and Arrays

JavaScript is an object-oriented language, and much of its power comes from using objects.

  • Arrays: Ordered collections of data.
  • Objects: Collections of key-value pairs.

let car = {
    make: "Toyota",
    model: "Camry",
    year: 2020
};

console.log(car.make); // Outputs: Toyota        

Conclusion

JavaScript is a powerful language that opens up many possibilities for web development. The best way to learn is to practice writing code and experimenting with different features and APIs.

要查看或添加评论,请登录

Megha Sahay的更多文章

  • Stack vs Heap - Understanding memory in C#

    Stack vs Heap - Understanding memory in C#

    Managing memory in important for any programming language. This impacts both reliability and performance of a…

    1 条评论
  • Performance improvement for React based apps

    Performance improvement for React based apps

    Hello everyone welcome to this weeks post. In this week we will discuss few points that will improve the performance of…

  • Understanding Ransomware

    Understanding Ransomware

    In this issue of newsletter we will explore ransomware. I will provide an introduction to ransomware.

    1 条评论
  • Web API - An Introduction

    Web API - An Introduction

    In this issue of newsletter we will explore the APIs. I will provide an introduction to API.

  • Docker - A beginners guide

    Docker - A beginners guide

    Docker is an open-source platform that allows developers to build, package, and deploy applications as lightweight…

  • C# Extension Methods

    C# Extension Methods

    In this week's newsletter we will take a closer look at extension methods in C#. By the end of this article you should…

    1 条评论
  • A Developer’s Dilemma: Selecting the Perfect Protocol - GraphQL, REST, gRPC or WebSockets?

    A Developer’s Dilemma: Selecting the Perfect Protocol - GraphQL, REST, gRPC or WebSockets?

    Selecting the ideal protocol for your unique Project Introduction Many software architects face problems to choose…

  • CQRS in C#

    CQRS in C#

    In this edition of newsletter we will understand what is CQRS pattern with a sample clean architecture example. By the…

  • Implementing ORM with .Net

    Implementing ORM with .Net

    We will look at implementing Object–relational mapping with Entity core framework. We will cover the following topics.

  • Using GraphQL with C#

    Using GraphQL with C#

    An application demonstrating use of Graphql with C#. In todays newsletter we will look into how can we use GraphQL with…

社区洞察

其他会员也浏览了