Thursday, June 20, 2024

LANGUAGE SYNTAX

 

Let's delve into the JavaScript language syntax, covering various aspects such as data types, variable declarations, differences between var and let, constants, dynamic typing, type checking, type conversion, objects, and arrays.

Data Types

JavaScript supports several primitive data types and a complex data type:

Primitive Data Types:

  • Number: Represents numeric values, e.g., 5, 3.14.
  • String: Represents textual data, e.g., 'Hello', "JavaScript".
  • Boolean: Represents true or false.
  • Undefined: Represents a variable that has been declared but has not been assigned a value.
  • Null: Represents an intentional absence of any object value.
  • Symbol (ES6): Represents a unique and immutable value.
  • Complex Data Type:


Object: Represents a collection of key-value pairs.

Variable Declarations

  • Variables are containers for storing data values. In JavaScript, variables can be declared using var, let, or const.

<script>
// Variable declaration with var
var age = 30;

// Variable declaration with let (block-scoped)
let name = 'Alice';

// Constant declaration (block-scoped, immutable)
const PI = 3.14;
</script>


var vs. let

var:

  • Function-scoped or globally scoped.
  • Can be re-declared and updated within its scope.


let:

  • Block-scoped (scoped within {}).
  • Cannot be re-declared within the same scope but can be updated.


Constants

  • Constants are declared using const and hold values that cannot be re-assigned.

const PI = 3.14;




Dynamic Typing

  • JavaScript is dynamically typed, meaning variables can hold values of any type without any type enforcement.

<script>

let dynamicVariable = 42; // Number
dynamicVariable = 'JavaScript'; // String

</script>


Type Checking

  • To check the type of a variable, you can use the typeof operator.

<script>

let x = 10;
console.log(typeof x); // Output: "number"

</script>


Type Conversion

JavaScript allows you to convert between different data types using explicit functions or automatic coercion.

  • Explicit Conversion

<script>

let numString = '42';
let num = Number(numString); // Convert string to number
console.log(num); // Output: 42

</script>


  • Automatic Coercion

<script>

let result = '3' + 2; // Concatenates as string
console.log(result); // Output: "32"

</script>


Objects

  • Objects are collections of key-value pairs where keys are strings (or symbols) and values can be any data type.

<script>

let person = {
name: 'Alice',
age: 30,
isStudent: true,
greet: function () {
return 'Hello!';
}
};

</script>


Arrays

  • Arrays are ordered collections of values (of any type) indexed by integers, starting from 0.
<script>

let fruits = ['Apple', 'Banana', 'Orange'];
console.log(fruits[0]); // Output: "Apple"

</script>


Example Combining Concepts

<script>

// Variable declarations
let message = 'Hello';
const PI = 3.14;

// Object with dynamic typing
let person = {
name: 'Alice',
age: 30,
isStudent: true,
greet: function () {
return 'Hello!';
}
};

// Array
let numbers = [1, 2, 3, 4, 5];

// Type conversion
let numString = '42';
let num = Number(numString); // Convert string to number
console.log(num); // Output: 42

// Type checking
let x = 10;
console.log(typeof x); // Output: "number"

</script>

No comments:

Post a Comment