Showing posts with label #SFDC #Apex #VineetYadav#JavaScript. Show all posts
Showing posts with label #SFDC #Apex #VineetYadav#JavaScript. Show all posts

Saturday, 31 December 2022

Basic JavaScript cheat

 / Declare a variable

let x;


// Assign a value to a variable

x = 5;


// Declare a variable and assign a value at the same time

let y = 10;


// Declare a constant

const PI = 3.14;


// Data types

let str = 'hello'; // string

let num = 42; // number

let bool = true; // boolean

let obj = {key: 'value'}; // object

let arr = [1, 2, 3]; // array

let func = function() {}; // function


// If statement

if (x > 10) {

  console.log('x is greater than 10');

} else {

  console.log('x is not greater than 10');

}


// For loop

for (let i = 0; i < 10; i++) {

  console.log(i);

}


// While loop

let j = 0;

while (j < 10) {

  console.log(j);

  j++;

}


// Do while loop

let k = 0;

do {

  console.log(k);

  k++;

} while (k < 10);


// Functions

function sayHello(name) {

  console.log(`Hello, ${name}!`);

}


sayHello('John');


// Arrow functions

let add = (x, y) => {

  return x + y;

};


console.log(add(5, 10));


// Objects

let person = {

  name: 'John',

  age: 30,

  sayHello: function() {

    console.log(`Hello, my name is ${this.name}`);

  }

};


console.log(person.name);

person.sayHello();


// Array methods

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


// forEach

numbers.forEach(function(num) {

  console.log(num);

});


// map

let doubled = numbers.map(function(num) {

  return num * 2;

});

console.log(doubled);


// filter

let evens = numbers.filter(function(num) {

  return num % 2 === 0;

});

console.log(evens);


// reduce

let sum = numbers.reduce(function(total, num) {

  return total + num;

}, 0);

console.log(sum);