This topic arose when a colleague from the website encountered a question during an interview: ‘What is the purpose of the JavaScript shift syntax for arrays?’
When they shared this with me, I also wasn’t sure about this syntax… I thought, ‘Am I falling behind?’
‘Shift’ seems like it would scramble the entire array, but to my surprise, after looking it up, it’s quite the opposite… Hahaha.
This article will introduce the uses of ‘shift’ and its partner ‘unshift,’ as well as highlight important considerations.
shift( ) – Removing the First Element
The shift( ) method is a built-in feature of JavaScript arrays that allows you to remove the first element of an array while also returning the removed element.
For example,
Consider an array: [“Apple”, “Orange”, “Pineapple”]
When we apply the shift( ) method to this array, the resulting array will contain only “Orange” and “Pineapple.”
Additionally, the shift( ) method also provides a return value, which is the element that has been removed.
let arr= ["Apple", "Orange", "Pineapple"]
let val = arr.shift()
console.log(val)
⬆️ You can try this js code and it will print “Apple.”
And this feature allows shift( ) to simulate the scenario of FIFO (First In First Out), which is akin to emulating the behavior of a queue.
Considerations:
Performance: While shift() can remove the first element, its performance might not be optimal for large arrays. This is due to the fact that every time an element is moved, the remaining elements need to be reindexed.
Empty Array: Applying shift() to an empty array will not result in an error, but it will return undefined.
unshift( ) – Adding Elements to the Beginning
The unshift( ) method allows you to add one or more elements to the beginning of an array and returns the new length of the array.
Example:
let arr= ["Orange", "Pineapple"];
let cnt = arr.unshift("Apple", "Banana");
console.log(arr); // ["Apple", "Banana", "Orange", "Pineapple"]
console.log(cnt); // 4
Considerations:
Performance: Similar to shift( ), if the array is large, the reindexing process during unshift( ) might be impacted.
No Elements Specified: If unshift( ) is used without specifying any elements to insert, the array will remain unaffected, and the method will return the original length of the array.
Conclusion
In reality, JavaScript has quite a depth of knowledge behind it. Most of the time, we tend to use JavaScript with the bare minimum standards, just aiming for it to work, haha.
That’s why it seems like there’s more note-taking involved in JavaScript compared to other languages, because there’s always new ground to cover.
var uagb_data = {"ajax_url":"https:\/\/zyrastory.com\/wp-admin\/admin-ajax.php","uagb_masonry_ajax_nonce":"34e1941e62"};
var uagb_data = {"ajax_url":"https:\/\/zyrastory.com\/wp-admin\/admin-ajax.php","uagb_masonry_ajax_nonce":"34e1941e62","uagb_grid_ajax_nonce":"833f7d8b8d"};
document.addEventListener("DOMContentLoaded", function(){
( function( $ ) {
var cols = parseInt( '3' );
var $scope = $( '.uagb-block-fa6afaef' );
let imagePosition = 'top';if( 'top' !== imagePosition ){
// This CSS is for Post BG Image Spacing
let articles = document.querySelectorAll( '.uagb-post__image-position-background .uagb-post__inner-wrap' );
if( articles.length ) {
for( let article of articles ) {
let image = article.getElementsByClassName('uagb-post__image');
if ( image[0] ) {
let articleWidth = article.offsetWidth;
let rowGap = 20;
let imageWidth = 100 - ( rowGap / articleWidth ) * 100;
image[0].style.width = imageWidth + '%';
image[0].style.marginLeft = rowGap / 2 + 'px';
}
}
}
}
// If this is not a Post Carousel, return.
// Else if it is a carousel but has less posts than the number of columns, return after setting visibility.
if ( ! $scope.hasClass('is-carousel') ) {
return;
} else if ( cols >= $scope.children('article.uagb-post__inner-wrap').length ) {
$scope.css( 'visibility', 'visible' );
return;
}
var slider_options = {
'slidesToShow' : cols,
'slidesToScroll' : 1,
'autoplaySpeed' : 2000,
'autoplay' : Boolean( '1' ),
'infinite' : Boolean( '1' ),
'pauseOnHover' : Boolean( '1' ),
'speed' : 500,
'arrows' : Boolean( '1' ),
'dots' : Boolean( '1' ),
'rtl' : Boolean( '' ),
'prevArrow' : '<button type=\"button\" data-role=\"none\" class=\"slick-prev\" aria-label=\"Previous\" tabindex=\"0\" role=\"button\"><svg width=\"20\" height=\"20\" viewBox=\"0 0 256 512\"><path d=\"M31.7 239l136-136c9.4-9.4 24.6-9.4 33.9 0l22.6 22.6c9.4 9.4 9.4 24.6 0 33.9L127.9 256l96.4 96.4c9.4 9.4 9.4 24.6 0 33.9L201.7 409c-9.4 9.4-24.6 9.4-33.9 0l-136-136c-9.5-9.4-9.5-24.6-.1-34z\"></path></svg><\/button>',
'nextArrow' : '<button type=\"button\" data-role=\"none\" class=\"slick-next\" aria-label=\"Next\" tabindex=\"0\" role=\"button\"><svg width=\"20\" height=\"20\" viewBox=\"0 0 256 512\"><path d=\"M224.3 273l-136 136c-9.4 9.4-24.6 9.4-33.9 0l-22.6-22.6c-9.4-9.4-9.4-24.6 0-33.9l96.4-96.4-96.4-96.4c-9.4-9.4-9.4-24.6 0-33.9L54.3 103c9.4-9.4 24.6-9.4 33.9 0l136 136c9.5 9.4 9.5 24.6.1 34z\"></path></svg><\/button>',
'responsive' : [
{
'breakpoint' : 1024,
'settings' : {
'slidesToShow' : 2,
'slidesToScroll' : 1,
}
},
{
'breakpoint' : 767,
'settings' : {
'slidesToShow' : 1,
'slidesToScroll' : 1,
}
}
]
};$scope.imagesLoaded( function() {
$scope.slick( slider_options );
}).always( function() {
$scope.css( 'visibility', 'visible' );
} );var enableEqualHeight = ( '1' );if( enableEqualHeight ){
$scope.imagesLoaded( function() {
UAGBPostCarousel?._setHeight( $scope );
});$scope.on( 'afterChange', function() {
UAGBPostCarousel?._setHeight( $scope );
} );
}} )( jQuery );
});
ai_front = {"insertion_before":"BEFORE","insertion_after":"AFTER","insertion_prepend":"PREPEND CONTENT","insertion_append":"APPEND CONTENT","insertion_replace_content":"REPLACE CONTENT","insertion_replace_element":"REPLACE ELEMENT","visible":"VISIBLE","hidden":"HIDDEN","fallback":"FALLBACK","automatically_placed":"Automatically placed by AdSense Auto ads code","cancel":"Cancel","use":"Use","add":"Add","parent":"Parent","cancel_element_selection":"Cancel element selection","select_parent_element":"Select parent element","css_selector":"CSS selector","use_current_selector":"Use current selector","element":"ELEMENT","path":"PATH","selector":"SELECTOR"};