JavaScript Fundamentals: Data Types, Variables, Operators, Control Statements, Loops, Functions, and In-Built Methods By Shivam Maurya

 

1. Data Types:


Primitive Data Types:

  • String: Represents a sequence of characters, enclosed in single (') or double (") quotes.

    javascript
    let greeting = "Hello, World!";
  • Number: Represents numeric values.

    javascript
    let age = 25;
  • Boolean: Represents either true or false.

    javascript
    let isStudent = true;
  • Undefined: Represents an uninitialized variable. If a variable is declared but not assigned a value, it is undefined.

    javascript
    let notDefined;
  • Null: Represents the intentional absence of any object value.

    javascript
    let noValue = null;

Non-Primitive Data Types:

  • Object: Represents a collection of key-value pairs.

    javascript
    let person = { name: "John", age: 30, isStudent: false };
  • Array: Represents an ordered list of values.

    javascript
    let fruits = ["apple", "banana", "orange"];
  • Function: Represents a reusable block of code.

    javascript
    function greet(name) { console.log("Hello, " + name + "!"); }

2. Variables:

Variables are used to store values. In JavaScript, you can declare variables using var, let, or const.

  • var (function-scoped): The original way to declare variables.
  • let (block-scoped): Introduced in ES6, allows reassignment.
  • const (block-scoped): Declares a variable with a constant value.
javascript
var myVar = "Hello"; let myNumber = 42; const pi = 3.14;

3. Operators:

  • Arithmetic Operators: Perform mathematical operations.

    javascript
    let a = 5; let b = 2; console.log(a + b); // Addition: 7
  • Comparison Operators: Compare values and return a boolean result.

    javascript
    let x = 10; let y = 5; console.log(x > y); // Greater than: true
  • Logical Operators: Perform logical operations.

    javascript
    let isTrue = true; let isFalse = false; console.log(isTrue && isFalse); // Logical AND: false

4. Control Conditional Statements:

  • if Statement: Executes a block of code if a specified condition is true.

    javascript
    let num = 10; if (num > 0) { console.log("Number is positive"); }
  • if...else Statement: Executes one block of code if the condition is true, and another if it's false.

    javascript
    let num = -5; if (num > 0) { console.log("Number is positive"); } else { console.log("Number is non-positive"); }
  • nested if...else Statement: Uses multiple if...else statements for complex conditions.

    javascript
    let num = 0; if (num > 0) { console.log("Number is positive"); } else if (num < 0) { console.log("Number is negative"); } else { console.log("Number is zero"); }
  • switch...case Statement: A switch statement allows a program to evaluate an expression and attempt to match the expression's value to a case label.

    javascript
    let day = "Monday"; switch (day) { case "Monday": console.log("It's the start of the week"); break; case "Friday": console.log("It's almost the weekend"); break; default: console.log("It's a regular day"); }

5. Loops:

  • for Loop: Executes a block of code a specified number of times.

    javascript
    for (let i = 0; i < 5; i++) { console.log(i); }
  • while Loop: Executes a block of code as long as a specified condition is true.

    javascript
    let i = 0; while (i < 5) { console.log(i); i++; }

6. Functions and Arrow Functions:

  • Function Declaration: Defines a function using the function keyword.

    javascript
    function greet(name) { console.log("Hello, " + name + "!"); } greet("John");
  • Arrow Function: A concise way to define functions introduced in ES6.

    javascript
    const greet = (name) => { console.log(`Hello, ${name}!`); }; greet("John");

7. In-built Methods and Properties:

  • length: Returns the length of a string or the number of elements in an array.

    javascript
    let str = "Hello, World!"; console.log(str.length); // Output: 13
  • typeof: Returns a string representing the type of a variable.

    javascript
    let num = 42; console.log(typeof num); // Output: "number"
  • join(): Joins all elements of an array into a string.

    javascript
    let arr = ["apple", "banana", "orange"]; let joinedStr = arr.join(", "); console.log(joinedStr); // Output: "apple, banana, orange"
Shivam Maurya

Shivam Maurya, a resident of Semaura, Husainganj, Fatehpur, Uttar Pradesh, India (212651), is a versatile individual with a passion for ethical hacking, blogging, and content creation. He completed his education from Jawahar Navodaya Vidyalaya, Sarkandi, Bindki, Fatehpur, showcasing a strong foundation in academics. Shivam possesses a diverse skill set, proficient in several programming languages such as HTML, CSS, Java, and JavaScript. Additionally, he's well-versed in operating systems like Parrot OS and Kali Linux, making him adept in the realm of cybersecurity. Shivam's expertise and interests converge in the world of blogging, where he curates engaging content that resonates with his audience. His in-depth knowledge and hands-on experience in ethical hacking provide valuable insights to his readers, enhancing their understanding of this critical field. Shivam Maurya is a passionate, tech-savvy individual dedicated to sharing his expertise, making him a valuable contributor to the tech and cybersecurity community.

Post a Comment

Previous Post Next Post