www.xbdev.net
xbdev - software development
Thursday January 2, 2025
Home | Contact | Support | JavaScript... so much power in such a few lines of code..
     
 

JavaScript...

so much power in such a few lines of code..

 


Javascript > Basic Javascript


JavaScript is a versatile programming language used primarily for web development;

Breakdown of the basic JavaScript syntax, structure, and layout.


1. Comments:
- Single-line comments start with `//`.
- Multi-line comments are enclosed between `/` and `/`.

// This is a single-line comment

/*
This is a
multi-line
comment
*/


2. Variables:
- Declared using `var`, `let`, or `const`.
- `var` is function-scoped, while `let` and `const` are block-scoped.
- `let` allows reassignment, while `const` creates a constant whose value cannot be changed once defined.

var 5;
let y 10;
const 
15;


3. Data Types:
- JavaScript has several data types, including numbers, strings, booleans, arrays, objects, etc.

let num 10;
let str "Hello";
let bool true;
let arr = [123];
let obj = { name"John"age30 };


4. Operators:
- Arithmetic operators (`+`, `-`, `*`, `/`, `%`).
- Comparison operators (`==`, `===`, `!=`, `!==`, `>`, `<`, `>=`, `<=`).
- Logical operators (`&&`, `||`, `!`).

let sum 3;
let isEqual = (sum === 8);
let isGreater = (sum 5);


5. Control Structures:
- Conditional statements (if-else).
- Loops (for, while, do-while).

if (condition) {
    
// Code block to execute if condition is true
} else {
    
// Code block to execute if condition is false
}

for (
let i 05i++) {
    
// Code block to execute repeatedly
}

while (
condition) {
    
// Code block to execute while condition is true
}


6. Functions:
- Functions are blocks of code that perform a specific task.
- They can be declared or expressed.

// Function declaration
function greet(name) {
    return 
"Hello, " name "!";
}

// Function expression
let greet = function(name) {
    return 
"Hello, " name "!";
};


7. Objects and Classes:
- Objects are collections of key-value pairs.
- Classes allow you to create objects with shared properties and methods.

// Object
let person = {
    
name"John",
    
age30,
    
greet: function() {
        return 
"Hello, my name is " this.name "!";
    }
};

// Class
class Person {
    
constructor(nameage) {
        
this.name name;
        
this.age age;
    }

    
greet() {
        return 
"Hello, my name is " this.name "!";
    }
}


Things to Try


• Create a single html file with a script - write a small program that demonstrates each of the '7' basic syntax, structure, and layout points discussed above.












 
Advert (Support Website)

 
 Visitor:
Copyright (c) 2002-2024 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.