What is var functionName = function() {} vs function functionName() {}

The difference is that functionOne is a function expression and so only defined when that line is reached, whereas functionTwo is a function declaration and is defined as soon as its surrounding function or script is executed (due to hoisting).

For example, a function expression:


Solution


// TypeError: functionOne is not a function


functionOne();

var functionOne = function() {
  console.log("Hello!");
};

And, a function declaration:


// Outputs: "Hello!"


functionTwo();

function functionTwo() {
  console.log("Hello!");
}

Historically, function declarations defined within blocks were handled inconsistently between browsers. Strict mode (introduced in ES5) resolved this by scoping function declarations to their enclosing block.

'use strict';    
{ // note this block!
  function functionThree() {
    console.log("Hello!");
  }
}
functionThree(); // ReferenceError

What is unshift in Javascript | Demo| Example

Here We can learn Unshift() method very easy with Example . Below we have given example and Demo you can check it let me know if you are getting any issue with unshift() method  The arrA.unshift() and arrB.unshift()  method is used to add one or more elements to the beginning of the given array. This function increases the length of the existing array by the number of elements added to the array.

Demo URL



Syntax: 

arrayUnshift.unshift(el1, el2, el2, ..., eX)

Parameters: This method accept a single parameters.


  • el: This parameter elements to be added at the beginning of the array.

Return value: This function returns the new length of the array after inserting the arguments at the beginning of the array.

Below examples illustrate the JavaScript ArrayUnshift unshift() method:

  • Example 1: In this example the function unshift() adds 28 and 65 to the front of the array.
    // Example 1
    var arrA = [5,26,39,45,90];
    console.log(arrA.unshift(27,39));
    console.log(arrA);
    

    Output:

  • Example 2: In this example the shift() method tries to remove the first element of the array, but the array is empty, therefore it returns undefined.
    // Example 2 
    var arrB = [20,45,75,80,90,100];
    console.log(arrB.unshift());
    console.log(arrB);
    

    Output:

How to add woocommerce cash on delivery extra fee without plugin

Note: All payment methods are only available on Checkout page.

The following code will add conditionally a specific fee based on the chosen payment method:

// Add a custom fee (fixed or based cart subtotal percentage) by payment
add_action( 'woocommerce_cart_calculate_fees', 'custom_handling_fee' );
function custom_handling_fee ( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $chosen_payment_id = WC()->session->get('chosen_payment_method');

    if ( empty( $chosen_payment_id ) )
        return;

    $subtotal = $cart->subtotal;

    // SETTINGS: Here set in the array the (payment Id) / (fee cost) pairs
    $targeted_payment_ids = array(
        'cod' => 8, // Fixed fee
        'paypal' => 5 * $subtotal / 100, // Percentage fee
    );

    // Loop through defined payment Ids array
    foreach ( $targeted_payment_ids as $payment_id => $fee_cost ) {
        if ( $chosen_payment_id === $payment_id ) {
            $cart->add_fee( __('Handling fee', 'woocommerce'), $fee_cost, true );
        }
    }
}

You will need the following to refresh checkout on payment method change, to get it work:

// jQuery - Update checkout on payment method change
add_action( 'woocommerce_checkout_init', 'payment_methods_refresh_checkout' );
function payment_methods_refresh_checkout() {
    wc_enqueue_js( "jQuery( function($){
        $('form.checkout').on('change', 'input[name=payment_method]', function(){
            $(document.body).trigger('update_checkout');
        });
    });");
}

Code goes in functions.php file of your active child theme (or active theme). tested and works.

People Who Code: What is a Temporal Dead Zone?

People Who Code: What is a Temporal Dead Zone?:   Temporal Dead Zone is a behaviour that occurs with variables declared using  let  and  const  keywords. It is a behaviour where we try to ...

What is a Temporal Dead Zone?

 Temporal Dead Zone is a behaviour that occurs with variables declared using let and const keywords.


It is a behaviour where we try to access a variable before it is initialized.

Examples of temporal dead zone:

x = 23; // Gives reference error

let x;


function anotherRandomFunc(){
  message = "Hello"; // Throws a reference error

  let message;
}
anotherRandomFunc();

In the code above, both in global scope and functional scope, we are trying to access variables which have not been declared yet. This is called the Temporal Dead Zone .

What is Object Destructuring?

 Object destructuring is a new way to extract elements from an object or an array.


Object destructuring:

Before ES6 version:

const classDetails = {
  strength: 78,
  benches: 39,
  blackBoard:1
}

const classStrength = classDetails.strength;
const classBenches = classDetails.benches;
const classBlackBoard = classDetails.blackBoard;

The same example using object destructuring:

const classDetails = {
  strength: 78,
  benches: 39,
  blackBoard:1
}

const {strength:classStrength, benches:classBenches,blackBoard:classBlackBoard} = classDetails;

console.log(classStrength); // Outputs 78
console.log(classBenches); // Outputs 39
console.log(classBlackBoard); // Outputs 1

As one can see, using object destructuring we have extracted all the elements inside an object in one line of code.

If we want our new variable to have the same name as the property of an object we can remove the colon:

const {strength:strength} = classDetails;
// The above line of code can be written as:
const {strength} = classDetails;

Array destructuring:


Before ES6 version:

const arr = [1, 2, 3, 4];
const first = arr[0];
const second = arr[1];
const third = arr[2];
const fourth = arr[3];

The same example using object destructuring:

const arr = [1, 2, 3, 4];
const [first,second,third,fourth] = arr;

console.log(first); // Outputs 1
console.log(second); // Outputs 2
console.log(third); // Outputs 3
console.log(fourth); // Outputs 4

Explain WeakMap in javascript.

In javascript, Map is used to store key-value pairs. The key-value pairs can be of both primitive and non-primitive types.

WeakMap is similar to Map with key differences:

  • The keys and values in weakmap should always be an object.
  • If there are no references to the object, the object will be garbage collected.



const map1 = new Map();
map1.set('Value', 1);

const map2 = new WeakMap();
map2.set('Value', 2.3); // Throws an error

let obj = {name:"Vijay"};
const map3 = new WeakMap();
map3.set(obj, {age:23});

CPU vs GPU Architecture

  CPU vs GPU Architecture CPU (Central Processing Unit) and GPU (Graphics Processing Unit) have distinct architectural differences, optimize...

Best for you