DEV Community

Cover image for JAVASCRIPT NECESSITIES(things you need to emphasis on):
Jane49-cloud
Jane49-cloud

Posted on

JAVASCRIPT NECESSITIES(things you need to emphasis on):

1.Variables: Variables are used to store and manipulate data in JavaScript. You can create a variable using the var, let, or const keyword.

let x = 5;
console.log(x); // Output: 5

Enter fullscreen mode Exit fullscreen mode

2.Data types: JavaScript has several data types, including numbers, strings, booleans, and objects.

let num = 5; // number
let str = 'Hello'; // string
let bool = true; // boolean
let obj = {key: 'value'}; // object


Enter fullscreen mode Exit fullscreen mode

3.Arrays: Arrays are used to store lists of data in JavaScript. You can access and modify the elements of an array using their index.

let arr = [1, 2, 3, 4, 5];
console.log(arr[2]); // Output: 3
arr[2] = 10;
console.log(arr); // Output: [1, 2, 10, 4, 5]

Enter fullscreen mode Exit fullscreen mode

4.Loops: Loops allow you to repeat a block of code multiple times. There are several types of loops in JavaScript, including for loops and while loops.

for (let i = 0; i < 5; i++) {
  console.log(i); // Output: 0 1 2 3 4
}

let j = 0;
while (j < 5) {
  console.log(j); // Output: 0 1 2 3 4
  j++;
}

Enter fullscreen mode Exit fullscreen mode

5.Functions: Functions are blocks of code that can be called by their name. They can also accept arguments and return a value.

function add(x, y) {
  return x + y;
}

console.log(add(2, 3)); // Output: 5

Enter fullscreen mode Exit fullscreen mode

6.Objects: Objects are collections of key-value pairs in JavaScript. You can access and modify the values of an object using its keys.

let obj = {
  key1: 'value1',
  key2: 'value2'
};

console.log(obj.key1); // Output: 'value1'
obj.key1 = 'new value';
console.log(obj.key1); // Output: 'new value'

Enter fullscreen mode Exit fullscreen mode

7.Destructuring: Destructuring is a feature of JavaScript that allows you to extract values from arrays and objects into variables.

let arr = [1, 2, 3];
let [x, y, z] = arr;
console.log(x); // Output: 1
console.log(y); // Output: 2
console.log(z); // Output: 3

let obj = {key1: 'value1', key2: 'value2'};
let {key1, key2} = obj;
console.log(key1); // Output: 'value1'
console.log(key2); // Output: 'value2'

Enter fullscreen mode Exit fullscreen mode

8.Spread operator: The spread operator allows you to spread the elements of an array or object into a new array or object.

let arr1 = [1, 2, 3];
let arr2 = [...arr1, 4, 5];
console.log(arr2); // Output: [1, 2, 3, 4, 5]

let obj1 = {key1:

Enter fullscreen mode Exit fullscreen mode

9 Map method: The map method is a higher-order function that allows you to transform the elements of an array. It returns a new array with the transformed elements.

let arr = [1, 2, 3];
let newArr = arr.map(x => x * 2);
console.log(newArr); // Output: [2, 4, 6]

Enter fullscreen mode Exit fullscreen mode

10.Set method: The Set object is a collection of unique values. You can use the add method to add values to a Set, and the has method to check if a value is in a Set.

let set = new Set();
set.add(1);
set.add(2);
set.add(3);

console.log(set.has(1)); // Output: true
console.log(set.has(4)); // Output: false

Enter fullscreen mode Exit fullscreen mode

Top comments (0)