<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>learning javascript Archives | Kernix Web Design</title>
	<atom:link href="https://kernixwebdesign.com/tag/learning-javascript/feed/" rel="self" type="application/rss+xml" />
	<link>https://kernixwebdesign.com/tag/learning-javascript/</link>
	<description>Website Design and Development</description>
	<lastBuildDate>Sun, 12 May 2024 18:13:50 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.2</generator>

<image>
	<url>https://kernixwebdesign.com/wp-content/uploads/2020/12/cropped-fav-icon3-32x32.png</url>
	<title>learning javascript Archives | Kernix Web Design</title>
	<link>https://kernixwebdesign.com/tag/learning-javascript/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Learn JavaScript: String and Array Methods</title>
		<link>https://kernixwebdesign.com/website/learn-javascript-string-array-methods/</link>
		
		<dc:creator><![CDATA[Jim Kernicky]]></dc:creator>
		<pubDate>Fri, 27 May 2022 00:09:05 +0000</pubDate>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Website]]></category>
		<category><![CDATA[array methods]]></category>
		<category><![CDATA[learning javascript]]></category>
		<category><![CDATA[string methods]]></category>
		<guid isPermaLink="false">https://kernixwebdesign.com/?p=20079</guid>

					<description><![CDATA[<p>When you first start to learn JavaScript you find that there are so many methods that it can be overwhelming. This article makes that process easier by covering the 7 methods and properties that are in common with JavaScript strings and arrays.</p>
<p>The post <a href="https://kernixwebdesign.com/website/learn-javascript-string-array-methods/">Learn JavaScript: String and Array Methods</a> appeared first on <a href="https://kernixwebdesign.com">Kernix Web Design</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>The number of methods and properties that you need to know and remember to solve JavaScript problems and build apps can be intimidating. In learning JavaScript, I noticed that there were method names that are used by both strings and arrays.</p>
<p>In this article, I cover 7 methods that are used by both arrays and strings. Learn these 7 methods and you actually have learned 14 JavaScript methods. I also cover an additional 7 methods that use different method names for the same or opposite purpose.</p>
<h2>String and Array methods in common</h2>
<p>The easiest set of methods to learn is the methods that work for both arrays and strings. There are 5 methods that have the same name and same effect for both strings and arrays. There are also two &#8220;properties&#8221; in common to both strings and arrays.</p>
<h3>The string and array slice() method</h3>
<p>Use slice to return a copy or a portion of a string or array without mutating the original.</p>
<p>Use <code>str.<span class="purple">slice</span>()</code> to return a copy of the string.</p>
<p>Use <code>arr.<span class="purple">slice</span>()</code> to return a copy of the array.</p>
<p>Both methods can be used without any arguments, with a start index only, or with both a start and ending index. For more information on the methods check out MDN <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice">String Slice</a> and <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice">Array Slice</a> pages.</p>
<h3 class="western">The string and array concat() method</h3>
<p>The concat method concatenates, or merges, two or more strings or arrays together by adding the value in the parentheses to the end of the calling string/array. Check out MDN <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/concat">String concat</a> and <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat">Array concat</a> for more information.</p>
<p>Use <code>str.<span class="purple">concat</span>(str2, str3, …)</code> to join 2 or more strings and return a copy of the joined strings.</p>
<p>Use <code>arr.<span class="purple">concat</span>(arr2, arr3, …)</code> to join 2 or more arrays and return a copy of the joined arrays.</p>
<p><strong>NOTE</strong>: Using the <code>+</code> and <code>+=</code> assignment operators used with strings accomplishes the same effect as <code><span class="purple">concat</span>()</code>. It is strongly recommended to use the assignment operators instead of the <code><span class="purple">concat</span>()</code> method with strings.</p>
<h3>The string and array includes() method</h3>
<p>The includes method is extremely useful, especially as a condition in if statements. Take a look at MDN <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes">string includes</a> and <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes">array includes</a> for syntax and examples.</p>
<p>Use <code>str.<span class="purple">includes</span>(value)</code> to perform a case-sensitive search to determine whether one string may be found within another string, returning true or false as appropriate.</p>
<p>Use <code>arr.<span class="purple">includes</span>(value)</code> to determine whether an array includes a certain value among its entries, returns true or false.</p>
<h3>The string and array indexOf() method</h3>
<p>The indexOf method is also extremely useful for various purposes. Check out MDN <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf">string indexOf</a> and <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf">array indexOf</a> for more examples and syntax.</p>
<p>Use <code>str.<span class="purple">indexOf</span>(value)</code> to return the first index at which a given element can be found in the string, or -1 if it is not present.</p>
<p>Use <code>arr.<span class="purple">indexOf</span>(value)</code> to return the first index at which a given element can be found in the array, or -1 if it is not present.</p>
<h3>The string and array lastIndexOf() method</h3>
<p>The lastIndexOf method works as the indexOf method but searches starting at the end of the string or array. Check the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/lastIndexOf">string lastIndexOf</a> and <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/lastIndexOf">array lastIndexOf</a> methods on MDN.</p>
<p>Use <code>str.<span class="purple">lastIndexOf</span>(value)</code> to return the last index at which a given element can be found in the string, or -1 if it is not present.</p>
<p>Use <code>str.<span class="purple">lastIndexOf</span>(value)</code> to return the last index at which a given element can be found in the array, or -1 if it is not present.</p>
<h3>Two properties in common: length and bracket notation</h3>
<p>The following two techniques are not methods but are common to both strings and arrays. Both .length and bracket notation are very useful and you probably are already aware of them, but I wanted to include them all the same.</p>
<p>Use <code>str.<span class="blue">length</span></code> and <code>arr.<span class="blue">length</span></code> to return the length of the string and array respectively.</p>
<p>Use <code>str[index]</code> and <code>arr[index]</code> to return the string character and array value at the specified index respectively.</p>
<p>&nbsp;</p>
<p><img fetchpriority="high" decoding="async" class="alignleft wp-image-20094 size-full" src="https://kernixwebdesign.com/wp-content/uploads/2022/05/learning-javascript.jpg" alt="Confidence with programming" width="400" height="268" srcset="https://kernixwebdesign.com/wp-content/uploads/2022/05/learning-javascript.jpg 400w, https://kernixwebdesign.com/wp-content/uploads/2022/05/learning-javascript-300x201.jpg 300w" sizes="(max-width: 400px) 100vw, 400px" /></p>
<h2>String and Array methods with different names but with similarities</h2>
<p>Here are some other methods that have different names but have the same or opposite results. Personally, I do not use the first two methods and have only occasionally used the substring method.</p>
<h3>charAt and at methods</h3>
<p>Using str.charAt and arr.at returns the value at the index similar to bracket notation. There are reasons why you would use the arr.at() method over bracket notation (see <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at">MDN array at method</a>), but I fail to see the application for the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charAt">string charAt</a> method.</p>
<p>Use <code>str.<span class="purple">charAt</span>(index)</code> to return the character at the index.</p>
<p>Use <code>arr.<span class="purple">at</span>(index)</code> to return the array item at the index.</p>
<h3>Substring and splice (and slice) methods</h3>
<p>Splice, slice, and substring all extract a part of an array or string. Substring and slice are identical with the exception of edge cases.</p>
<p>Use <code>str.<span class="purple">substring</span>()</code> to return a part of a string.</p>
<p>Use <code>arr.<span class="purple">splice</span>()</code> to return part of an array.</p>
<p>Slice and substring do not mutate the original source, but arr.splice() does. Check out <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring">MDN substring</a> and <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice">array splice</a> pages for more information. You should use all 3 methods on strings and arrays where applicable to understand the differences.</p>
<h3>Split vs. Join methods</h3>
<p>The split method is fantastic when working with strings. I believe I have only used the array join method in conjunction with split.</p>
<p>Use <code>str.<span class="purple">split</span>()</code> to turn a string into an array of substrings.</p>
<p>Use <code>arr.<span class="purple">join</span>()</code> to turn an array into a string.</p>
<p><strong>NOTE</strong>: These two are often used in tandem. A common problem you will see is to turn a page or blog post title into a URL slug with a dash (-) as a separator. freeCodeCamp has an intermediate algorithm called <em>Spinal Tap</em> for that scenario but with some tricky test cases.</p>
<p>Check out the MDN pages on <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split">string split</a> and <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join">array join</a> for more information.</p>
<h2>Final Thoughts</h2>
<p>Hopefully, comparing JavaScript string and array methods like this has made learning JavaScript a little easier.</p>
<p>There are a handful of other string methods you should learn, and a lot more array methods as well. Let this article start you on the road to improving your understanding of JavaScript.</p>
<p>I have a repository on Github called <a href="https://github.com/Kernix13/javascript-cheat-sheet">JavaScript Cheat Sheet</a> though it is way bigger than a cheat sheet, and is still a work-in-progress. Take a look at it and start building your own JavaScript cheat sheet to help you get coding better and faster!</p>
<p>The post <a href="https://kernixwebdesign.com/website/learn-javascript-string-array-methods/">Learn JavaScript: String and Array Methods</a> appeared first on <a href="https://kernixwebdesign.com">Kernix Web Design</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
