Talk:JavaScript Programming/Variables and Expressions

Variables and Expressions

edit

1. Variable Declarations:

  • let: Used to declare a variable that can be reassigned later. Example

let name = 'Alice';

name = 'Bob'; // Reassigns the value

·        const: Used to declare a variable that cannot be reassigned after its initial assignment. Example:

let name = 'Alice';

name = 'Bob'; // Reassigns the value

2. Properties and Methods for Strings:

·        Strings in JavaScript have various properties and methods. For example:


let str = 'Hello, world!';

console.log(str.length); // 13

console.log(str.toUpperCase()); // 'HELLO, WORLD!'

console.log(str.indexOf('world')); // 7


3. Primitive Data Types:

·        JavaScript has several primitive data types: number, string, boolean, null, undefined, symbol, and bigint.

               let num = 42; // number

let str = 'Hello'; // string

let isTrue = true; // boolean

let nothing = null; // null

let notDefined; // undefined

4. Operators:

·        String Concatenation: You can concatenate strings using the + operator:

let firstName = 'John';

let lastName = 'Doe';

let fullName = firstName + ' ' + lastName; // 'John Doe'


·        Arithmetic Operators: Operators used for mathematical calculations:

let sum = 5 + 3; // 8

let difference = 10 - 4; // 6

let product = 2 * 6; // 12

let quotient = 8 / 2; // 4

let remainder = 7 % 3; // 1


·        Assignment Operators: Used to assign values to variables. Some common ones:

let x = 10; // Simple assignment

x += 5; // Equivalent to x = x + 5

x -= 2; // Equivalent to x = x – 2


·        Increment Operators: Used to increase a variable’s value:

let count = 0;

count++; // Equivalent to count = count + 1

count--; // Equivalent to count = count – 1


·        Comparison Operators: Used to compare values:

let a = 5;

let b = 10;

console.log(a == b); // false

console.log(a != b); // true

console.log(a > b); // false

console.log(a < b); // true

console.log(a >= 5); // true

console.log(a <= 5); // true


·        Logical Operators: Used to combine or invert boolean values:

let x = true;

let y = false;

console.log(x && y); // false (AND)

console.log(x || y); // true (OR)

console.log(!x); // false (NOT) Ravtantu (discusscontribs) 20:35, 8 September 2024 (UTC)Reply

Return to "JavaScript Programming/Variables and Expressions" page.