Top 10 Coding JavaScript Examples
Top 10 Coding JavaScript Examples
JavaScript Code
'use strict';
// Use let
let name = 'John Doe';
// Use const
const PI = 3.14;
// Function expression
const multiply = (a, b) => a * b;
// Destructuring arrays
const colors = ['red', 'green', 'blue'];
const [first, second, third] = colors;
// Destructuring objects
const person = {
name: 'John Doe',
age: 30,
job: 'Software Engineer'
};
const { name, age, job } = person;
// Use map
const double = numbers.map(number => number * 2);
// Use filter
const even = numbers.filter(number => number % 2 ===
0);
// Use reduce
const sum = numbers.reduce((acc, number) => acc +
number, 0);
// math.js
export const PI = 3.14;
export const add = (a, b) => a + b;
// main.js
import { PI, add } from './math.js';
console.log(PI); // 3.14
console.log(add(1, 2)); // 3
// Default value
let name = 'John Doe';