<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: thai-duong2003</title>
    <description>The latest articles on DEV Community by thai-duong2003 (@thaiduong2003).</description>
    <link>https://dev.to/thaiduong2003</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2294755%2F9383fe12-1b79-4f05-b64f-895bdfbf044f.png</url>
      <title>DEV Community: thai-duong2003</title>
      <link>https://dev.to/thaiduong2003</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/thaiduong2003"/>
    <language>en</language>
    <item>
      <title>Chuỗi (String) strong Jacascript</title>
      <dc:creator>thai-duong2003</dc:creator>
      <pubDate>Sun, 22 Dec 2024 17:22:09 +0000</pubDate>
      <link>https://dev.to/thaiduong2003/chuoi-string-strong-jacascript-1kek</link>
      <guid>https://dev.to/thaiduong2003/chuoi-string-strong-jacascript-1kek</guid>
      <description>&lt;h2&gt;
  
  
  I. Khái quát
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Khái niệm
&lt;/h3&gt;

&lt;p&gt;chuỗi (string) là một kiểu dữ liệu được sử dụng để lưu trữ một chuỗi ký tự có thể bao gồm chữ cái, số, ký hiệu, từ hoặc câu .&lt;br&gt;
Chuỗi là một tập hợp các ký tự được bao bọc bởi dấu nháy đơn (') hoặc dấu nháy kép ("), và từ ES6, bạn cũng có thể sử dụng template literals với dấu backtick .&lt;/p&gt;
&lt;h3&gt;
  
  
  2. Tạo chuỗi
&lt;/h3&gt;

&lt;p&gt;Có 3 cách để tạo chính để tạo chuỗi:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sử dụng Dấu Nháy Đơn hoặc Dấu Nháy Kép:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;let &lt;/span&gt;singleQuote &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Hello, world!'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;let &lt;/span&gt;doubleQuote &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Hello, world!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Sử dụng Constructor &lt;code&gt;String&lt;/code&gt;:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;let &lt;/span&gt;str &lt;span class="o"&gt;=&lt;/span&gt; String&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Hello, world!"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Chuyển đổi sang chuỗi
&lt;span class="nb"&gt;let &lt;/span&gt;anotherStr &lt;span class="o"&gt;=&lt;/span&gt; new String&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Hello, world"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Tạo đối tượng chuỗi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Lưu ý: Trong trường hợp sử dụng dấu nháy đơn &lt;code&gt;'&lt;/code&gt; và dấu nháy kép &lt;code&gt;""&lt;/code&gt; trong chuỗi bạn cần sử dụng thêm ký tự backslash () để có thể hiển thị chúng.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;let &lt;/span&gt;escapeSingle &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'It\'&lt;/span&gt;s a sunny day.&lt;span class="s1"&gt;'; // It'&lt;/span&gt;s a sunny day.
&lt;span class="nb"&gt;let &lt;/span&gt;escapeDouble &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"He said, &lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;Hello!&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // He said, &lt;span class="s2"&gt;"Hello!"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Dấu nháy ngược &lt;code&gt;&lt;/code&gt;&lt;code&gt;&lt;/code&gt; (Template literals):
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;let &lt;/span&gt;str1 &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"World"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;let &lt;/span&gt;str2 &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sb"&gt;`&lt;/span&gt;Hello &lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;str1&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Template literals, hỗ trợ nội suy
console.log&lt;span class="o"&gt;(&lt;/span&gt;str2&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Output: Hello World
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  II. Làm việc với chuỗi
&lt;/h2&gt;

&lt;h4&gt;
  
  
  1. Kiểm tra độ dài chuỗi (length)
&lt;/h4&gt;

&lt;p&gt;cú pháp: &lt;code&gt;string.length&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;let &lt;/span&gt;text &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"JavaScript"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
console.log&lt;span class="o"&gt;(&lt;/span&gt;text.length&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Output: 10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  2. Tìm kiếm chuỗi
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Tìm vị trí xuất hiện đầu tiên:&lt;/strong&gt;&lt;br&gt;
cú pháp: &lt;code&gt;string.indexOf(searchValue, start)&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;searchValue: Chuỗi con cần tìm.&lt;/li&gt;
&lt;li&gt;start (tùy chọn): Vị trí bắt đầu tìm kiếm trong chuỗi cha (mặc định là 0).
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;let &lt;/span&gt;text &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Hello, world!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
console.log&lt;span class="o"&gt;(&lt;/span&gt;text.indexOf&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"world"&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Output: 7
console.log&lt;span class="o"&gt;(&lt;/span&gt;text.indexOf&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"JavaScript"&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Output: &lt;span class="nt"&gt;-1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Tìm vị trí xuất hiện cuối cùng:&lt;/strong&gt;&lt;br&gt;
cú pháp: &lt;code&gt;string.lastIndexOf(searchValue, start)&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;searchValue: Chuỗi con cần tìm.&lt;/li&gt;
&lt;li&gt;start (tùy chọn): Vị trí bắt đầu tìm kiếm trong chuỗi cha (mặc định là 0).
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;let &lt;/span&gt;text &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Hello, world!, Hello, world!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
console.log&lt;span class="o"&gt;(&lt;/span&gt;text.lastIndexOf&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"world"&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Output: 22
console.log&lt;span class="o"&gt;(&lt;/span&gt;text.lastIndexOf&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Python"&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Output: &lt;span class="nt"&gt;-1&lt;/span&gt;
console.log&lt;span class="o"&gt;(&lt;/span&gt;text.lastIndexOf&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"world"&lt;/span&gt;, 10&lt;span class="o"&gt;))&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Output: 7

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Lưu ý:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Nếu chuỗi con không được tìm thấy, indexOf() sẽ trả về giá trị -1.&lt;/li&gt;
&lt;li&gt;indexOf() chỉ tìm được vị trí đầu tiên mà chuỗi con xuất hiện. Javascript sẽ bỏ qua những vị trí xuất hiện sau đó.&lt;/li&gt;
&lt;li&gt;lastIndexOf() chỉ tìm được vị trí cuối mà chuỗi con xuất hiện. Javascript sẽ bỏ qua những vị trí xuất hiện trước đó.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  4. Cắt chuỗi (slice)
&lt;/h4&gt;

&lt;p&gt;Cú pháp: &lt;code&gt;string.slice(startIndex, endIndex)&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;startIndex: Vị trí bắt đầu (tính từ 0).&lt;/li&gt;
&lt;li&gt;endIndex (tùy chọn): Vị trí kết thúc, không bao gồm ký tự tại endIndex. Nếu bỏ qua, chuỗi sẽ được cắt từ startIndex đến hết.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;
&lt;span class="nb"&gt;let &lt;/span&gt;text &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"JavaScript is awesome"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
console.log&lt;span class="o"&gt;(&lt;/span&gt;text.slice&lt;span class="o"&gt;(&lt;/span&gt;0, 10&lt;span class="o"&gt;))&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  // Output: &lt;span class="s2"&gt;"JavaScript"&lt;/span&gt;
console.log&lt;span class="o"&gt;(&lt;/span&gt;text.slice&lt;span class="o"&gt;(&lt;/span&gt;11&lt;span class="o"&gt;))&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;     // Output: &lt;span class="s2"&gt;"is awesome"&lt;/span&gt;
console.log&lt;span class="o"&gt;(&lt;/span&gt;text.slice&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;-7&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;     // Output: &lt;span class="s2"&gt;"awesome"&lt;/span&gt;
console.log&lt;span class="o"&gt;(&lt;/span&gt;text.slice&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;-7&lt;/span&gt;, &lt;span class="nt"&gt;-1&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Output: &lt;span class="s2"&gt;"awesom"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h4&gt;
  
  
  5. Thay Thế (replace)
&lt;/h4&gt;

&lt;p&gt;Cú pháp: &lt;code&gt;string.replace(searchValue, newValue)&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;searchValue: Chuỗi con cần tìm.&lt;/li&gt;
&lt;li&gt;newValue: Chuỗi thay thế.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Lưu ý:&lt;/strong&gt; Phương thức replace() có phân biệt chữ hoa và chữ thường&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;let &lt;/span&gt;text &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Hello, world!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;let &lt;/span&gt;newText1 &lt;span class="o"&gt;=&lt;/span&gt; text.replace&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"World"&lt;/span&gt;, &lt;span class="s2"&gt;"JavaScript"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;let &lt;/span&gt;newText2 &lt;span class="o"&gt;=&lt;/span&gt; text.replace&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"world"&lt;/span&gt;, &lt;span class="s2"&gt;"JavaScript"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
console.log&lt;span class="o"&gt;(&lt;/span&gt;newText1&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Output: &lt;span class="s2"&gt;"Hello, world!"&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;không thay thế vì &lt;span class="s2"&gt;"world"&lt;/span&gt; khác &lt;span class="s2"&gt;"World"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
console.log&lt;span class="o"&gt;(&lt;/span&gt;newText2&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Output: &lt;span class="s2"&gt;"Hello, JavaScript!"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  6. Chuyển đổi chữ hoa và chữ thường
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;string.toUpperCase()&lt;/code&gt;: Chuyển thành chữ in hoa&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;string.toLowerCase()&lt;/code&gt;: Chuyển thành chữ thường
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;let &lt;/span&gt;text &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"JavaScript"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
console.log&lt;span class="o"&gt;(&lt;/span&gt;text.toUpperCase&lt;span class="o"&gt;())&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Output: JAVASCRIPT
console.log&lt;span class="o"&gt;(&lt;/span&gt;text.toLowerCase&lt;span class="o"&gt;())&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Output: javascript
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  7. Kiểm tra chuỗi con
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;string.includes(searchString, position)&lt;/code&gt;: kiểm tra có giá trị trong chuỗi không.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;string.startsWith(searchString, position)&lt;/code&gt;: kiểm tra giá trị bắt đầu.&lt;/li&gt;
&lt;li&gt;s&lt;code&gt;tring.endsWith(searchString, length)&lt;/code&gt;: kiểm tra giá trị kết thúc.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;let &lt;/span&gt;text &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"JavaScript is fun"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
console.log&lt;span class="o"&gt;(&lt;/span&gt;text.includes&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"fun"&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Output: &lt;span class="nb"&gt;true
&lt;/span&gt;console.log&lt;span class="o"&gt;(&lt;/span&gt;text.startsWith&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Java"&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Output: &lt;span class="nb"&gt;true
&lt;/span&gt;console.log&lt;span class="o"&gt;(&lt;/span&gt;text.endsWith&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"fun"&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Output: &lt;span class="nb"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  8. Tách chuỗi
&lt;/h4&gt;

&lt;p&gt;Dựa vào điểm chung để cắt chuỗi và trả về dạng array.&lt;/p&gt;

&lt;p&gt;Cú pháp: &lt;code&gt;string.split(separator)&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;separator: Ký tự hoặc điểm chung. Nếu không chỉ định, chuỗi sẽ không bị tách.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;let &lt;/span&gt;text &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Apple, Banana, Orange"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;let &lt;/span&gt;fruits &lt;span class="o"&gt;=&lt;/span&gt; text.split&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;", "&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
console.log&lt;span class="o"&gt;(&lt;/span&gt;fruits&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Output: &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"Apple"&lt;/span&gt;, &lt;span class="s2"&gt;"Banana"&lt;/span&gt;, &lt;span class="s2"&gt;"Orange"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  9. Nối chuỗi
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;let &lt;/span&gt;text1 &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Hello"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;let &lt;/span&gt;text2 &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"World"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;let &lt;/span&gt;result &lt;span class="o"&gt;=&lt;/span&gt; text1 + &lt;span class="s2"&gt;" "&lt;/span&gt; + text2&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;let &lt;/span&gt;result2 &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;text1&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt; &lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;text2&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Template literals
console.log&lt;span class="o"&gt;(&lt;/span&gt;result&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Output: Hello World
console.log&lt;span class="o"&gt;(&lt;/span&gt;result2&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Output: Hello World
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  10. Xóa khoảng trắng
&lt;/h4&gt;

&lt;p&gt;Cú pháp: &lt;code&gt;string.trim()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;let &lt;/span&gt;text &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"   JavaScript   "&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
console.log&lt;span class="o"&gt;(&lt;/span&gt;text.trim&lt;span class="o"&gt;())&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Output: JavaScript
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  10. Chuyển đổi kiểu
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;let &lt;/span&gt;str &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"123"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;let &lt;/span&gt;num &lt;span class="o"&gt;=&lt;/span&gt; 123&lt;span class="p"&gt;;&lt;/span&gt;
console.log&lt;span class="o"&gt;(&lt;/span&gt;Number&lt;span class="o"&gt;(&lt;/span&gt;str&lt;span class="o"&gt;))&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Output: 123
console.log&lt;span class="o"&gt;(&lt;/span&gt;parseInt&lt;span class="o"&gt;(&lt;/span&gt;str&lt;span class="o"&gt;))&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Output: 123
console.log&lt;span class="o"&gt;(&lt;/span&gt;String&lt;span class="o"&gt;(&lt;/span&gt;num&lt;span class="o"&gt;))&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Output: &lt;span class="s2"&gt;"123"&lt;/span&gt;
console.log&lt;span class="o"&gt;(&lt;/span&gt;num.toString&lt;span class="o"&gt;())&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Output: &lt;span class="s2"&gt;"123"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  III. Lời kết
&lt;/h2&gt;

&lt;p&gt;Trong bài viết này, mình đã giới thiệu cho các bạn những điều cơ bản về cách làm việc với chuỗi trong JavaScript, từ việc tạo và hiển thị các ký tự chuỗi và các phương thức phổ biến khi làm việc với chuỗi trong JavaScript.&lt;br&gt;
Hy vọng bài viết trên có thể giúp đỡ các bạn trong việc hiểu thêm về JavaScript.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Chuỗi (String) strong Jacascript</title>
      <dc:creator>thai-duong2003</dc:creator>
      <pubDate>Sun, 22 Dec 2024 17:22:09 +0000</pubDate>
      <link>https://dev.to/thaiduong2003/chuoi-string-strong-jacascript-109h</link>
      <guid>https://dev.to/thaiduong2003/chuoi-string-strong-jacascript-109h</guid>
      <description>&lt;h2&gt;
  
  
  I. Khái quát
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Khái niệm
&lt;/h3&gt;

&lt;p&gt;chuỗi (string) là một kiểu dữ liệu được sử dụng để lưu trữ một chuỗi ký tự có thể bao gồm chữ cái, số, ký hiệu, từ hoặc câu .&lt;br&gt;
Chuỗi là một tập hợp các ký tự được bao bọc bởi dấu nháy đơn (') hoặc dấu nháy kép ("), và từ ES6, bạn cũng có thể sử dụng template literals với dấu backtick .&lt;/p&gt;
&lt;h3&gt;
  
  
  2. Tạo chuỗi
&lt;/h3&gt;

&lt;p&gt;Có 3 cách để tạo chính để tạo chuỗi:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sử dụng Dấu Nháy Đơn hoặc Dấu Nháy Kép:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;let &lt;/span&gt;singleQuote &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Hello, world!'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;let &lt;/span&gt;doubleQuote &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Hello, world!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Sử dụng Constructor &lt;code&gt;String&lt;/code&gt;:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;let &lt;/span&gt;str &lt;span class="o"&gt;=&lt;/span&gt; String&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Hello, world!"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Chuyển đổi sang chuỗi
&lt;span class="nb"&gt;let &lt;/span&gt;anotherStr &lt;span class="o"&gt;=&lt;/span&gt; new String&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Hello, world"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Tạo đối tượng chuỗi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Lưu ý: Trong trường hợp sử dụng dấu nháy đơn &lt;code&gt;'&lt;/code&gt; và dấu nháy kép &lt;code&gt;""&lt;/code&gt; trong chuỗi bạn cần sử dụng thêm ký tự backslash () để có thể hiển thị chúng.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;let &lt;/span&gt;escapeSingle &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'It\'&lt;/span&gt;s a sunny day.&lt;span class="s1"&gt;'; // It'&lt;/span&gt;s a sunny day.
&lt;span class="nb"&gt;let &lt;/span&gt;escapeDouble &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"He said, &lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;Hello!&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // He said, &lt;span class="s2"&gt;"Hello!"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Dấu nháy ngược &lt;code&gt;&lt;/code&gt;&lt;code&gt;&lt;/code&gt; (Template literals):
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;let &lt;/span&gt;str1 &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"World"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;let &lt;/span&gt;str2 &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sb"&gt;`&lt;/span&gt;Hello &lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;str1&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Template literals, hỗ trợ nội suy
console.log&lt;span class="o"&gt;(&lt;/span&gt;str2&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Output: Hello World
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  II. Làm việc với chuỗi
&lt;/h2&gt;

&lt;h4&gt;
  
  
  1. Kiểm tra độ dài chuỗi (length)
&lt;/h4&gt;

&lt;p&gt;cú pháp: &lt;code&gt;string.length&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;let &lt;/span&gt;text &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"JavaScript"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
console.log&lt;span class="o"&gt;(&lt;/span&gt;text.length&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Output: 10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  2. Tìm kiếm chuỗi
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Tìm vị trí xuất hiện đầu tiên:&lt;/strong&gt;&lt;br&gt;
cú pháp: &lt;code&gt;string.indexOf(searchValue, start)&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;searchValue: Chuỗi con cần tìm.&lt;/li&gt;
&lt;li&gt;start (tùy chọn): Vị trí bắt đầu tìm kiếm trong chuỗi cha (mặc định là 0).
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;let &lt;/span&gt;text &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Hello, world!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
console.log&lt;span class="o"&gt;(&lt;/span&gt;text.indexOf&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"world"&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Output: 7
console.log&lt;span class="o"&gt;(&lt;/span&gt;text.indexOf&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"JavaScript"&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Output: &lt;span class="nt"&gt;-1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Tìm vị trí xuất hiện cuối cùng:&lt;/strong&gt;&lt;br&gt;
cú pháp: &lt;code&gt;string.lastIndexOf(searchValue, start)&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;searchValue: Chuỗi con cần tìm.&lt;/li&gt;
&lt;li&gt;start (tùy chọn): Vị trí bắt đầu tìm kiếm trong chuỗi cha (mặc định là 0).
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;let &lt;/span&gt;text &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Hello, world!, Hello, world!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
console.log&lt;span class="o"&gt;(&lt;/span&gt;text.lastIndexOf&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"world"&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Output: 22
console.log&lt;span class="o"&gt;(&lt;/span&gt;text.lastIndexOf&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Python"&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Output: &lt;span class="nt"&gt;-1&lt;/span&gt;
console.log&lt;span class="o"&gt;(&lt;/span&gt;text.lastIndexOf&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"world"&lt;/span&gt;, 10&lt;span class="o"&gt;))&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Output: 7

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Lưu ý:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Nếu chuỗi con không được tìm thấy, indexOf() sẽ trả về giá trị -1.&lt;/li&gt;
&lt;li&gt;indexOf() chỉ tìm được vị trí đầu tiên mà chuỗi con xuất hiện. Javascript sẽ bỏ qua những vị trí xuất hiện sau đó.&lt;/li&gt;
&lt;li&gt;lastIndexOf() chỉ tìm được vị trí cuối mà chuỗi con xuất hiện. Javascript sẽ bỏ qua những vị trí xuất hiện trước đó.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  4. Cắt chuỗi (slice)
&lt;/h4&gt;

&lt;p&gt;Cú pháp: &lt;code&gt;string.slice(startIndex, endIndex)&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;startIndex: Vị trí bắt đầu (tính từ 0).&lt;/li&gt;
&lt;li&gt;endIndex (tùy chọn): Vị trí kết thúc, không bao gồm ký tự tại endIndex. Nếu bỏ qua, chuỗi sẽ được cắt từ startIndex đến hết.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;
&lt;span class="nb"&gt;let &lt;/span&gt;text &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"JavaScript is awesome"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
console.log&lt;span class="o"&gt;(&lt;/span&gt;text.slice&lt;span class="o"&gt;(&lt;/span&gt;0, 10&lt;span class="o"&gt;))&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  // Output: &lt;span class="s2"&gt;"JavaScript"&lt;/span&gt;
console.log&lt;span class="o"&gt;(&lt;/span&gt;text.slice&lt;span class="o"&gt;(&lt;/span&gt;11&lt;span class="o"&gt;))&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;     // Output: &lt;span class="s2"&gt;"is awesome"&lt;/span&gt;
console.log&lt;span class="o"&gt;(&lt;/span&gt;text.slice&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;-7&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;     // Output: &lt;span class="s2"&gt;"awesome"&lt;/span&gt;
console.log&lt;span class="o"&gt;(&lt;/span&gt;text.slice&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;-7&lt;/span&gt;, &lt;span class="nt"&gt;-1&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Output: &lt;span class="s2"&gt;"awesom"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h4&gt;
  
  
  5. Thay Thế (replace)
&lt;/h4&gt;

&lt;p&gt;Cú pháp: &lt;code&gt;string.replace(searchValue, newValue)&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;searchValue: Chuỗi con cần tìm.&lt;/li&gt;
&lt;li&gt;newValue: Chuỗi thay thế.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Lưu ý:&lt;/strong&gt; Phương thức replace() có phân biệt chữ hoa và chữ thường&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;let &lt;/span&gt;text &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Hello, world!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;let &lt;/span&gt;newText1 &lt;span class="o"&gt;=&lt;/span&gt; text.replace&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"World"&lt;/span&gt;, &lt;span class="s2"&gt;"JavaScript"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;let &lt;/span&gt;newText2 &lt;span class="o"&gt;=&lt;/span&gt; text.replace&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"world"&lt;/span&gt;, &lt;span class="s2"&gt;"JavaScript"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
console.log&lt;span class="o"&gt;(&lt;/span&gt;newText1&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Output: &lt;span class="s2"&gt;"Hello, world!"&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;không thay thế vì &lt;span class="s2"&gt;"world"&lt;/span&gt; khác &lt;span class="s2"&gt;"World"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
console.log&lt;span class="o"&gt;(&lt;/span&gt;newText2&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Output: &lt;span class="s2"&gt;"Hello, JavaScript!"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  6. Chuyển đổi chữ hoa và chữ thường
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;string.toUpperCase()&lt;/code&gt;: Chuyển thành chữ in hoa&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;string.toLowerCase()&lt;/code&gt;: Chuyển thành chữ thường
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;let &lt;/span&gt;text &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"JavaScript"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
console.log&lt;span class="o"&gt;(&lt;/span&gt;text.toUpperCase&lt;span class="o"&gt;())&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Output: JAVASCRIPT
console.log&lt;span class="o"&gt;(&lt;/span&gt;text.toLowerCase&lt;span class="o"&gt;())&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Output: javascript
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  7. Kiểm tra chuỗi con
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;string.includes(searchString, position)&lt;/code&gt;: kiểm tra có giá trị trong chuỗi không.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;string.startsWith(searchString, position)&lt;/code&gt;: kiểm tra giá trị bắt đầu.&lt;/li&gt;
&lt;li&gt;s&lt;code&gt;tring.endsWith(searchString, length)&lt;/code&gt;: kiểm tra giá trị kết thúc.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;let &lt;/span&gt;text &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"JavaScript is fun"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
console.log&lt;span class="o"&gt;(&lt;/span&gt;text.includes&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"fun"&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Output: &lt;span class="nb"&gt;true
&lt;/span&gt;console.log&lt;span class="o"&gt;(&lt;/span&gt;text.startsWith&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Java"&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Output: &lt;span class="nb"&gt;true
&lt;/span&gt;console.log&lt;span class="o"&gt;(&lt;/span&gt;text.endsWith&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"fun"&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Output: &lt;span class="nb"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  8. Tách chuỗi
&lt;/h4&gt;

&lt;p&gt;Dựa vào điểm chung để cắt chuỗi và trả về dạng array.&lt;/p&gt;

&lt;p&gt;Cú pháp: &lt;code&gt;string.split(separator)&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;separator: Ký tự hoặc điểm chung. Nếu không chỉ định, chuỗi sẽ không bị tách.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;let &lt;/span&gt;text &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Apple, Banana, Orange"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;let &lt;/span&gt;fruits &lt;span class="o"&gt;=&lt;/span&gt; text.split&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;", "&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
console.log&lt;span class="o"&gt;(&lt;/span&gt;fruits&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Output: &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"Apple"&lt;/span&gt;, &lt;span class="s2"&gt;"Banana"&lt;/span&gt;, &lt;span class="s2"&gt;"Orange"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  9. Nối chuỗi
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;let &lt;/span&gt;text1 &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Hello"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;let &lt;/span&gt;text2 &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"World"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;let &lt;/span&gt;result &lt;span class="o"&gt;=&lt;/span&gt; text1 + &lt;span class="s2"&gt;" "&lt;/span&gt; + text2&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;let &lt;/span&gt;result2 &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;text1&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt; &lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;text2&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="sb"&gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Template literals
console.log&lt;span class="o"&gt;(&lt;/span&gt;result&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Output: Hello World
console.log&lt;span class="o"&gt;(&lt;/span&gt;result2&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Output: Hello World
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  10. Xóa khoảng trắng
&lt;/h4&gt;

&lt;p&gt;Cú pháp: &lt;code&gt;string.trim()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;let &lt;/span&gt;text &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"   JavaScript   "&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
console.log&lt;span class="o"&gt;(&lt;/span&gt;text.trim&lt;span class="o"&gt;())&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Output: JavaScript
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  10. Chuyển đổi kiểu
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;let &lt;/span&gt;str &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"123"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;let &lt;/span&gt;num &lt;span class="o"&gt;=&lt;/span&gt; 123&lt;span class="p"&gt;;&lt;/span&gt;
console.log&lt;span class="o"&gt;(&lt;/span&gt;Number&lt;span class="o"&gt;(&lt;/span&gt;str&lt;span class="o"&gt;))&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Output: 123
console.log&lt;span class="o"&gt;(&lt;/span&gt;parseInt&lt;span class="o"&gt;(&lt;/span&gt;str&lt;span class="o"&gt;))&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Output: 123
console.log&lt;span class="o"&gt;(&lt;/span&gt;String&lt;span class="o"&gt;(&lt;/span&gt;num&lt;span class="o"&gt;))&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Output: &lt;span class="s2"&gt;"123"&lt;/span&gt;
console.log&lt;span class="o"&gt;(&lt;/span&gt;num.toString&lt;span class="o"&gt;())&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Output: &lt;span class="s2"&gt;"123"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  III. Lời kết
&lt;/h2&gt;

&lt;p&gt;Trong bài viết này, mình đã giới thiệu cho các bạn những điều cơ bản về cách làm việc với chuỗi trong JavaScript, từ việc tạo và hiển thị các ký tự chuỗi và các phương thức phổ biến khi làm việc với chuỗi trong JavaScript.&lt;br&gt;
Hy vọng bài viết trên có thể giúp đỡ các bạn trong việc hiểu thêm về JavaScript.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Toán tử trong Javascript</title>
      <dc:creator>thai-duong2003</dc:creator>
      <pubDate>Sun, 22 Dec 2024 09:54:39 +0000</pubDate>
      <link>https://dev.to/thaiduong2003/toan-tu-trong-javascript-4n4j</link>
      <guid>https://dev.to/thaiduong2003/toan-tu-trong-javascript-4n4j</guid>
      <description>&lt;h2&gt;
  
  
  I. Toán tử là gì?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Toán tử là các dấu hay ký tự đặc biệt, dùng để thực hiện các thao tác trên giá trị hoặc biến nào đó để cho ra kết quả cuối cùng.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  II. Các loại toán tử
&lt;/h2&gt;

&lt;p&gt;Trong JavaScript có nhiều loại toán tử mà bạn nên biết như :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Toán tử số học - Arithmetic Operators&lt;/li&gt;
&lt;li&gt;Toán tử gán - Assignment Operators&lt;/li&gt;
&lt;li&gt;Toán tử so sánh - Comparison Operators&lt;/li&gt;
&lt;li&gt;Toán tử logic - Logical Operators&lt;/li&gt;
&lt;li&gt;Toán tử chuỗi - String Operators&lt;/li&gt;
&lt;li&gt;Toán tử ba ngôi - ternary operator&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  1. Toán tử số học - Arithmetic Operators
&lt;/h4&gt;

&lt;p&gt;Toán tử số học trong JavaScript là các ký hiệu dùng để thực hiện các phép tính toán học cơ bản trên số. Chúng giúp xử lý các phép toán như cộng, trừ, nhân, chia...&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Toán tử&lt;/th&gt;
&lt;th&gt; Mô tả&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;+&lt;/td&gt;
&lt;td&gt;Cộng&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;td&gt;Trừ&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;*&lt;/td&gt;
&lt;td&gt;Nhân&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;**&lt;/td&gt;
&lt;td&gt;Nhân lũy thừa&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;/&lt;/td&gt;
&lt;td&gt;chia&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;%&lt;/td&gt;
&lt;td&gt;Chia lấy dư&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;++&lt;/td&gt;
&lt;td&gt;Tăng giá trị lên 1 đơn vị&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;--&lt;/td&gt;
&lt;td&gt;Giảm giá trị xuống 1 đơn vị&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Ví dụ :&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;let &lt;/span&gt;x &lt;span class="o"&gt;=&lt;/span&gt; 10&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;let &lt;/span&gt;y &lt;span class="o"&gt;=&lt;/span&gt; 3&lt;span class="p"&gt;;&lt;/span&gt;

console.log&lt;span class="o"&gt;(&lt;/span&gt;x + y&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  // 13
console.log&lt;span class="o"&gt;(&lt;/span&gt;x - y&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  // 7
console.log&lt;span class="o"&gt;(&lt;/span&gt;x &lt;span class="k"&gt;*&lt;/span&gt; y&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  // 30
console.log&lt;span class="o"&gt;(&lt;/span&gt;x / y&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  // 3.3333333333333335
console.log&lt;span class="o"&gt;(&lt;/span&gt;x % y&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  // 1
console.log&lt;span class="o"&gt;(&lt;/span&gt;x &lt;span class="k"&gt;**&lt;/span&gt; y&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // 1000
x++&lt;span class="p"&gt;;&lt;/span&gt;
console.log&lt;span class="o"&gt;(&lt;/span&gt;x&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;      // 11
x--&lt;span class="p"&gt;;&lt;/span&gt;
console.log&lt;span class="o"&gt;(&lt;/span&gt;x&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;      // 10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  2. Toán tử gán - Assignment Operators
&lt;/h4&gt;

&lt;p&gt;Toán tử gán được sử dụng để gán giá trị cho một biến. Toán tử gán cơ bản là &lt;code&gt;=&lt;/code&gt;, nhưng JavaScript cũng cung cấp nhiều toán tử gán kết hợp để thực hiện các phép tính và gán giá trị cùng lúc.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Toán tử&lt;/th&gt;
&lt;th&gt; Mô tả&lt;/th&gt;
&lt;th&gt; Tương đương&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;=&lt;/td&gt;
&lt;td&gt;x = y&lt;/td&gt;
&lt;td&gt;x = y&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;+=&lt;/td&gt;
&lt;td&gt;x += y&lt;/td&gt;
&lt;td&gt;x = x + y&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;-=&lt;/td&gt;
&lt;td&gt;x -= y&lt;/td&gt;
&lt;td&gt;x = x - y&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;*=&lt;/td&gt;
&lt;td&gt;x *= y&lt;/td&gt;
&lt;td&gt;x = x * y&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;/=&lt;/td&gt;
&lt;td&gt;x /= y&lt;/td&gt;
&lt;td&gt;x = x / y&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;%=&lt;/td&gt;
&lt;td&gt;x %= y&lt;/td&gt;
&lt;td&gt;x = x % y&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;**=&lt;/td&gt;
&lt;td&gt;x **= y&lt;/td&gt;
&lt;td&gt;x = x**y&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Ví dụ:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;let &lt;/span&gt;x &lt;span class="o"&gt;=&lt;/span&gt; 10&lt;span class="p"&gt;;&lt;/span&gt;

x +&lt;span class="o"&gt;=&lt;/span&gt; 5&lt;span class="p"&gt;;&lt;/span&gt;  // x &lt;span class="o"&gt;=&lt;/span&gt; 10 + 5 &lt;span class="o"&gt;(&lt;/span&gt;x &lt;span class="o"&gt;=&lt;/span&gt; 15&lt;span class="o"&gt;)&lt;/span&gt;
x -&lt;span class="o"&gt;=&lt;/span&gt; 3&lt;span class="p"&gt;;&lt;/span&gt;  // x &lt;span class="o"&gt;=&lt;/span&gt; 15 - 3 &lt;span class="o"&gt;(&lt;/span&gt;x &lt;span class="o"&gt;=&lt;/span&gt; 12&lt;span class="o"&gt;)&lt;/span&gt;
x &lt;span class="k"&gt;*&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; 2&lt;span class="p"&gt;;&lt;/span&gt;  // x &lt;span class="o"&gt;=&lt;/span&gt; 12 &lt;span class="k"&gt;*&lt;/span&gt; 2 &lt;span class="o"&gt;(&lt;/span&gt;x &lt;span class="o"&gt;=&lt;/span&gt; 24&lt;span class="o"&gt;)&lt;/span&gt;
x /&lt;span class="o"&gt;=&lt;/span&gt; 4&lt;span class="p"&gt;;&lt;/span&gt;  // x &lt;span class="o"&gt;=&lt;/span&gt; 24 / 4 &lt;span class="o"&gt;(&lt;/span&gt;x &lt;span class="o"&gt;=&lt;/span&gt; 6&lt;span class="o"&gt;)&lt;/span&gt;
x %&lt;span class="o"&gt;=&lt;/span&gt; 5&lt;span class="p"&gt;;&lt;/span&gt;  // x &lt;span class="o"&gt;=&lt;/span&gt; 6 % 5 &lt;span class="o"&gt;(&lt;/span&gt;x &lt;span class="o"&gt;=&lt;/span&gt; 1&lt;span class="o"&gt;)&lt;/span&gt;
x &lt;span class="k"&gt;**&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; 3&lt;span class="p"&gt;;&lt;/span&gt; // x &lt;span class="o"&gt;=&lt;/span&gt; 1 &lt;span class="k"&gt;**&lt;/span&gt; 3 &lt;span class="o"&gt;(&lt;/span&gt;x &lt;span class="o"&gt;=&lt;/span&gt; 1&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  3. Toán tử so sánh - Comparison Operators
&lt;/h4&gt;

&lt;p&gt;Dùng để so sánh hai giá trị và trả về một kết quả kiểu boolean (true hoặc false). Chúng thường được dùng trong các điều kiện hoặc các biểu thức logic.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Toán tử&lt;/th&gt;
&lt;th&gt; Mô tả&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;==&lt;/td&gt;
&lt;td&gt;So sánh bằng theo giá trị&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;===&lt;/td&gt;
&lt;td&gt;So sánh bằng cả giá trị và kiểu&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;!=&lt;/td&gt;
&lt;td&gt;So sánh không bằng theo giá trị&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;!==&lt;/td&gt;
&lt;td&gt;So sánh không bằng theo cả giá trị và kiểu&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&amp;gt;&lt;/td&gt;
&lt;td&gt;Lớn hơn&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&amp;lt;&lt;/td&gt;
&lt;td&gt;Bé hơn&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&amp;gt;=&lt;/td&gt;
&lt;td&gt;Lớn hơn hoặc bằng&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&amp;lt;=&lt;/td&gt;
&lt;td&gt;Bé hơn hoặc bằng&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Lưu ý:&lt;/strong&gt; khi sử dụng &lt;code&gt;==&lt;/code&gt; &lt;code&gt;===&lt;/code&gt; và &lt;code&gt;!=&lt;/code&gt; &lt;code&gt;!==&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;== không so sánh kiểu dữ liệu, do đó nó sẽ tự động chuyển đổi (coerce) kiểu dữ liệu để so sánh.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;=== yêu cầu cả giá trị và kiểu phải giống nhau.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;console.log&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"5"&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; 5&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // &lt;span class="nb"&gt;true
&lt;/span&gt;console.log&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"5"&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; 5&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // &lt;span class="nb"&gt;false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;!=&lt;/code&gt; và &lt;code&gt;!==&lt;/code&gt; tương tự
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;console.log&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"5"&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; 5&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // &lt;span class="nb"&gt;false
&lt;/span&gt;console.log&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"5"&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; 5&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // &lt;span class="nb"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  4. Toán tử logic - Logical Operators
&lt;/h4&gt;

&lt;p&gt;Toán tử logic được sử dụng để thực hiện các phép toán logic, thường dùng để kết hợp nhiều điều kiện hoặc biểu thức và trả về kết quả dạng boolean (true hoặc false) Rất phổ biến trong các cấu trúc điều khiển như if-else, while, hoặc trong các điều kiện phức tạp.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Toán tử&lt;/th&gt;
&lt;th&gt; Tên &lt;/th&gt;
&lt;th&gt; Mô tả&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&amp;amp;&amp;amp;&lt;/td&gt;
&lt;td&gt;Toán tử AND&lt;/td&gt;
&lt;td&gt;Trả về &lt;code&gt;true&lt;/code&gt; nếu tất cả các biểu thức đều đúng, ngược lại trả về &lt;code&gt;false&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;\&lt;/td&gt;
&lt;td&gt;\&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;!&lt;/td&gt;
&lt;td&gt;Toán tử NOT&lt;/td&gt;
&lt;td&gt;Đảo ngược giá trị logic của một biểu thức. &lt;br&gt; Nếu biểu thức là &lt;code&gt;true&lt;/code&gt;, kết quả sẽ là &lt;code&gt;false&lt;/code&gt;, và ngược lại.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;// AND
console.log&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;true&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   // &lt;span class="nb"&gt;true
&lt;/span&gt;console.log&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;true&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  // &lt;span class="nb"&gt;false
&lt;/span&gt;console.log&lt;span class="o"&gt;(&lt;/span&gt;5 &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; 3 &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; 10 &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; 8&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // &lt;span class="nb"&gt;true&lt;/span&gt;
// OR
console.log&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;true&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  // &lt;span class="nb"&gt;true
&lt;/span&gt;console.log&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;false&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // &lt;span class="nb"&gt;false
&lt;/span&gt;console.log&lt;span class="o"&gt;(&lt;/span&gt;5 &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; 10 &lt;span class="o"&gt;||&lt;/span&gt; 8 &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; 3&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // &lt;span class="nb"&gt;true&lt;/span&gt;
// NOT
console.log&lt;span class="o"&gt;(!&lt;/span&gt;&lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    // &lt;span class="nb"&gt;false
&lt;/span&gt;console.log&lt;span class="o"&gt;(!&lt;/span&gt;&lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   // &lt;span class="nb"&gt;true
&lt;/span&gt;console.log&lt;span class="o"&gt;(!(&lt;/span&gt;5 &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; 3&lt;span class="o"&gt;))&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // &lt;span class="nb"&gt;false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  5. Toán tử nối chuỗi
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Toán tử nối chuỗi (+): dừng để nối các chuỗi và biến lại với nhau
Cú pháp: chuỗi1 + chuỗi2
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;let &lt;/span&gt;firstName &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"John"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;let &lt;/span&gt;lastName &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Doe"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
console.log&lt;span class="o"&gt;(&lt;/span&gt;firstName + &lt;span class="s2"&gt;" "&lt;/span&gt; + lastName&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // &lt;span class="s2"&gt;"John Doe"&lt;/span&gt;
console.log&lt;span class="o"&gt;(&lt;/span&gt;5 + &lt;span class="s2"&gt;"5"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;// 55
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lưu ý: Nếu một trong các toán hạng là số và toán hạng còn lại là chuỗi, thì JavaScript sẽ chuyển số thành chuỗi trước khi nối&lt;/p&gt;

&lt;h4&gt;
  
  
  6. Toán tử ba ngôi - ternary operator
&lt;/h4&gt;

&lt;p&gt;Toán tử ba ngôi là một cách ngắn gọn để viết các câu lệnh điều kiện if-else một cách ngắn gọn. Đây là toán tử duy nhất trong JavaScript có ba toán hạng.&lt;br&gt;
Điều kiện ? giá trị 1 : giá trị 2&lt;/p&gt;

&lt;p&gt;giá trị 1: Giá trị hoặc biểu thức được thực thi nếu Điều kiện là true.&lt;br&gt;
giá trị 2: Giá trị hoặc biểu thức được thực thi nếu Điều kiện là false.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;let &lt;/span&gt;age &lt;span class="o"&gt;=&lt;/span&gt; 20&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;let &lt;/span&gt;access &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;age &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; 18&lt;span class="o"&gt;)&lt;/span&gt; ? &lt;span class="s2"&gt;"Được phép truy cập"&lt;/span&gt; : &lt;span class="s2"&gt;"Bạn chưa đủ tuổi"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
console.log&lt;span class="o"&gt;(&lt;/span&gt;access&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // &lt;span class="s2"&gt;"Được phép truy cập"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ưu điểm:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ngắn gọn và dễ viết: Giúp mã ngắn hơn khi xử lý điều kiện đơn giản.&lt;/li&gt;
&lt;li&gt;Trực quan hơn: Khi viết mã ngắn gọn, dễ hiểu hơn.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Nhược điểm:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Khó đọc: Nếu lồng nhiều toán tử ba ngôi, mã sẽ trở nên khó hiểu.&lt;/li&gt;
&lt;li&gt;Hạn chế với logic phức tạp: Với các điều kiện phức tạp, if-else sẽ rõ ràng hơn.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  7. Lời kết
&lt;/h4&gt;

&lt;p&gt;Trên đây là các loại toán tử phổ biến thường gặp, ngoài ra trong Js còn một số toán tử khác mang tính chuyên dụng hơn các bạn có thể chủ động tìm hiểu thêm nhé. Để giải quyết các bài toán lập trình với Javascript việc nắm vững các toán tử Js là yêu cầu bắt buộc đối với các lập trình viên.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Tìm hiểu Object trong JavaScript</title>
      <dc:creator>thai-duong2003</dc:creator>
      <pubDate>Sat, 21 Dec 2024 15:33:11 +0000</pubDate>
      <link>https://dev.to/thaiduong2003/tim-hieu-object-trong-javascript-1an9</link>
      <guid>https://dev.to/thaiduong2003/tim-hieu-object-trong-javascript-1an9</guid>
      <description>&lt;h1&gt;
  
  
  1. Khái niệm Object trong JavaScript?
&lt;/h1&gt;

&lt;p&gt;Object là một kiểu dữ liệu cho phép bạn lưu trữ và quản lý dữ liệu dưới dạng các cặp key-value. Nó được sử dụng để biểu diễn các thực thể trong thế giới thực hoặc các cấu trúc dữ liệu phức tạp.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Đặc điểm cơ bản của Object:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Cấu trúc dạng key-value: Mỗi thuộc tính (property) của Object là một cặp key (tên thuộc tính) và value (giá trị).&lt;/li&gt;
&lt;li&gt;Động: Bạn có thể thêm, sửa đổi hoặc xóa các thuộc tính bất kỳ lúc nào.&lt;/li&gt;
&lt;li&gt;Linh hoạt: Các giá trị trong Object có thể là bất kỳ kiểu dữ liệu nào, bao gồm cả số, chuỗi, hàm, mảng hoặc thậm chí là một Object khác.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  3. Khởi tạo Object
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;có 3 cách khởi tạo một object:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Dùng dấu ngoặc nhọn &lt;code&gt;{}&lt;/code&gt; : đây là cách phổ biến
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt; const person &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    name: &lt;span class="s2"&gt;"Alice"&lt;/span&gt;,
    age: 25,
    greet: &lt;span class="k"&gt;function&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        console.log&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Hello, "&lt;/span&gt; + this.name&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  console.log&lt;span class="o"&gt;(&lt;/span&gt;person.name&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Alice
  person.greet&lt;span class="o"&gt;()&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Hello, Alice
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Sử dụng từ khóa &lt;code&gt;new Object()&lt;/code&gt;: Đây là cách tạo Object thông qua constructor Object.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;const person &lt;span class="o"&gt;=&lt;/span&gt; new Object&lt;span class="o"&gt;()&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
person.name &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Alice"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
person.age &lt;span class="o"&gt;=&lt;/span&gt; 25&lt;span class="p"&gt;;&lt;/span&gt;
console.log&lt;span class="o"&gt;(&lt;/span&gt;person&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // &lt;span class="o"&gt;{&lt;/span&gt;name:&lt;span class="s2"&gt;"Alice"&lt;/span&gt;,age &lt;span class="o"&gt;=&lt;/span&gt; 25&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Sử dụng phương thức &lt;code&gt;Object.create()&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;const proto &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; greet: &lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; console.log&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Hello!"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
const person &lt;span class="o"&gt;=&lt;/span&gt; Object.create&lt;span class="o"&gt;(&lt;/span&gt;proto&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
person.name &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Bob"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
console.log&lt;span class="o"&gt;(&lt;/span&gt;person.name&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Bob
person.greet&lt;span class="o"&gt;()&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Hello!

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  4. object Constructor
&lt;/h2&gt;

&lt;p&gt;Được hiểu là việc sử dụng một hàm constructor để tạo ra các đối tượng với cấu trúc và hành vi giống nhau. Cách sử dụng này cho phép bạn tái sử dụng mã và tạo ra các đối tượng với các thuộc tính và phương thức chung&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="k"&gt;function &lt;/span&gt;Person&lt;span class="o"&gt;(&lt;/span&gt;name, age&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    this.name &lt;span class="o"&gt;=&lt;/span&gt; name&lt;span class="p"&gt;;&lt;/span&gt;
    this.age &lt;span class="o"&gt;=&lt;/span&gt; age&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="nb"&gt;let &lt;/span&gt;person1 &lt;span class="o"&gt;=&lt;/span&gt; new Person&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Alice"&lt;/span&gt;, 25&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;let &lt;/span&gt;person2 &lt;span class="o"&gt;=&lt;/span&gt; new Person&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Bob"&lt;/span&gt;, 30&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

console.log&lt;span class="o"&gt;(&lt;/span&gt;person1&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Person &lt;span class="o"&gt;{&lt;/span&gt; name: &lt;span class="s1"&gt;'Alice'&lt;/span&gt;, age: 25 &lt;span class="o"&gt;}&lt;/span&gt;
console.log&lt;span class="o"&gt;(&lt;/span&gt;person2&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // Person &lt;span class="o"&gt;{&lt;/span&gt; name: &lt;span class="s1"&gt;'Bob'&lt;/span&gt;, age: 30 &lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Ưu điểm&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Tái sử dụng mã&lt;/li&gt;
&lt;li&gt;Tạo đối tượng với cấu trúc xác định&lt;/li&gt;
&lt;li&gt;Quản lý đối tượng dễ dàng hơn&lt;/li&gt;
&lt;li&gt;Hỗ trợ kế thừa&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Nhược điểm&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cú pháp phức tạp hơn so với cách tạo đối tượng bằng dấu ngoặc nhọn &lt;code&gt;{}&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Không linh hoạt cho các đối tượng đơn giản&lt;/li&gt;
&lt;li&gt;Khó khăn trong việc kế thừa (đối với các hàm constructor truyền thống)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  5. Các thao tác với Object
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Truy xuất giá trị thuộc tính:&lt;/strong&gt; Sử dụng dấu chấm &lt;code&gt;.&lt;/code&gt; hoặc dấu ngoặc vuông &lt;code&gt;[]&lt;/code&gt; để truy xuất giá trị thuộc tính.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;const person &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  name: &lt;span class="s2"&gt;"Alice"&lt;/span&gt;,
  age: 25,
&lt;span class="o"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

// Sử dụng dấu chấm
console.log&lt;span class="o"&gt;(&lt;/span&gt;person.name&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // &lt;span class="s2"&gt;"Alice"&lt;/span&gt;
console.log&lt;span class="o"&gt;(&lt;/span&gt;person.age&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  // 25

// Sử dụng dấu ngoặc vuông &lt;span class="o"&gt;(&lt;/span&gt;có thể sử dụng khi tên thuộc tính chứa ký tự đặc biệt hoặc biến&lt;span class="o"&gt;)&lt;/span&gt;
console.log&lt;span class="o"&gt;(&lt;/span&gt;person[&lt;span class="s2"&gt;"name"&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // &lt;span class="s2"&gt;"Alice"&lt;/span&gt;
console.log&lt;span class="o"&gt;(&lt;/span&gt;person[&lt;span class="s2"&gt;"age"&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  // 25
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Thêm, sửa, xóa thuộc tính:&lt;/strong&gt; Chúng ta cũng có thể cập nhật thuộc tính trong Object.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;const person &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  name: &lt;span class="s2"&gt;"Alice"&lt;/span&gt;,
  age: 25,
&lt;span class="o"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

// Thêm thuộc tính mới
person.address &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"123 Main St"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
console.log&lt;span class="o"&gt;(&lt;/span&gt;person&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // &lt;span class="o"&gt;{&lt;/span&gt; name: &lt;span class="s2"&gt;"Alice"&lt;/span&gt;, age: 25, address &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"123 Main St"&lt;/span&gt; &lt;span class="o"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

// Sửa giá trị thuộc tính
person.age &lt;span class="o"&gt;=&lt;/span&gt; 26&lt;span class="p"&gt;;&lt;/span&gt;
console.log&lt;span class="o"&gt;(&lt;/span&gt;person.age&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // 26

// Xóa thuộc tính
delete person.age&lt;span class="p"&gt;;&lt;/span&gt;
console.log&lt;span class="o"&gt;(&lt;/span&gt;person.age&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // undefined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Kiểm tra sự tồn tại của thuộc tính:&lt;/strong&gt; Sử dụng toán tử &lt;code&gt;in&lt;/code&gt; hoặc phương thức &lt;code&gt;hasOwnProperty()&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;const person &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  name: &lt;span class="s2"&gt;"Alice"&lt;/span&gt;,
&lt;span class="o"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

// Toán tử &lt;span class="k"&gt;in
&lt;/span&gt;console.log&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"name"&lt;/span&gt; &lt;span class="k"&gt;in &lt;/span&gt;person&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  // &lt;span class="nb"&gt;true
&lt;/span&gt;console.log&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"address"&lt;/span&gt; &lt;span class="k"&gt;in &lt;/span&gt;person&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  // &lt;span class="nb"&gt;false&lt;/span&gt;

// hasOwnProperty&lt;span class="o"&gt;()&lt;/span&gt;
console.log&lt;span class="o"&gt;(&lt;/span&gt;person.hasOwnProperty&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"name"&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // &lt;span class="nb"&gt;true
&lt;/span&gt;console.log&lt;span class="o"&gt;(&lt;/span&gt;person.hasOwnProperty&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"address"&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; // &lt;span class="nb"&gt;false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Hướng dẫn sử dụng GitHub cơ bản</title>
      <dc:creator>thai-duong2003</dc:creator>
      <pubDate>Mon, 28 Oct 2024 15:33:01 +0000</pubDate>
      <link>https://dev.to/thaiduong2003/huong-dan-su-dung-github-co-ban-255m</link>
      <guid>https://dev.to/thaiduong2003/huong-dan-su-dung-github-co-ban-255m</guid>
      <description>&lt;h1&gt;
  
  
  Hướng dẫn sử dụng GitHub cơ bản
&lt;/h1&gt;

&lt;h2&gt;
  
  
  I. Giới thiệu chung về GitHub
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. GitHub là gì?
&lt;/h3&gt;

&lt;p&gt;GitHub là một dịch vụ lưu trữ mã nguồn (source code) trực tuyến dựa trên nền tảng Git. Nó cung cấp một nền tảng cho các nhà phát triển để phát triển, quản lý và theo dõi mã nguồn của dự án phần mềm. Dịch vụ này cho phép các nhóm phát triển làm việc cùng nhau trên các dự án từ xa và theo dõi lịch sử của mã nguồn.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Một vài khái niệm của Git bạn cần nắm
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Repository&lt;/strong&gt;: Kho quản lý dữ liệu, là nơi lưu trữ các dữ liệu, mã nguồn của project.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Git&lt;/strong&gt;: Là tiền tố của các lệnh được sử dụng trên giao diện dòng lệnh (CLI).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Branch (nhánh)&lt;/strong&gt;: Là cơ chế giúp quản lý các phiên bản khác nhau của mã nguồn trong cùng một kho lưu trữ (repository). Mỗi branch đại diện cho một dòng phát triển riêng biệt của dự án, cho phép bạn làm việc trên các tính năng, sửa lỗi hoặc thử nghiệm ý tưởng mới mà không ảnh hưởng đến mã nguồn chính (thường được lưu trữ trên branch &lt;code&gt;main&lt;/code&gt; hoặc &lt;code&gt;master&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Clone&lt;/strong&gt;: Là lệnh được sử dụng để tạo ra một bản sao của một kho lưu trữ từ một máy chủ về máy tính cục bộ.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Commit&lt;/strong&gt;: Lưu lại các thay đổi trong mã nguồn vào kho lưu trữ. Mỗi commit giống như một điểm chụp lại trạng thái hiện tại của mã nguồn tại một thời điểm cụ thể.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Push&lt;/strong&gt;: Là thao tác để đẩy các thay đổi từ repository cục bộ lên repository trên máy chủ.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pull&lt;/strong&gt;: Lệnh này giúp bạn lấy về (fetch) và hợp nhất (merge) các thay đổi mới nhất từ một nhánh trên remote.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;.gitignore&lt;/strong&gt;: File được sử dụng để loại bỏ các thư mục, file mà mình không muốn push lên máy chủ Git.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  II. Hướng dẫn sử dụng GitHub
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Tạo tài khoản
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Bước 1&lt;/strong&gt;: Truy cập &lt;a href="https://github.com/" rel="noopener noreferrer"&gt;trang chủ GitHub&lt;/a&gt; và chọn &lt;strong&gt;Sign up&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Bước 2&lt;/strong&gt;: Nhập thông tin tài khoản email và mật khẩu, sau đó chọn &lt;strong&gt;Continue&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Bước 3&lt;/strong&gt;: Xác minh tài khoản để chứng minh là người thật.

&lt;ul&gt;
&lt;li&gt;Có 2 phương thức xác thực:

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Xác minh bằng hình ảnh&lt;/strong&gt;: Chọn &lt;strong&gt;Verify&lt;/strong&gt;, sau đó chọn hướng của hình ảnh theo yêu cầu và nhấn &lt;strong&gt;Submit&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Xác thực bằng âm thanh&lt;/strong&gt;: Chọn biểu tượng tai nghe, nghe và điền đáp án vào ô input rồi chọn &lt;strong&gt;Done&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Bước 4&lt;/strong&gt;: Sau khi hoàn tất xác minh người thật, bạn sẽ cần xác minh email. GitHub sẽ gửi một dãy số vào email của bạn, chỉ cần nhập vào để hoàn thành bước này.&lt;/li&gt;

&lt;/ul&gt;




&lt;h3&gt;
  
  
  2. Cài đặt SSH Key cho tài khoản
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Tại sao nên dùng SSH key thay vì HTTPS khi khởi tạo repo và commit?
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Từ tháng 8/2021, GitHub không còn hỗ trợ xác thực bằng mật khẩu qua HTTPS mà yêu cầu sử dụng &lt;strong&gt;Personal Access Token (PAT)&lt;/strong&gt;. Token này phải được tạo từ GitHub và thay thế mật khẩu. Nếu bạn chưa tạo và sử dụng token, thì push qua HTTPS sẽ không thành công.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  SSH key là gì?
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;SSH key là một cặp khóa mã hóa gồm một khóa công khai và một khóa bí mật. Chúng được sử dụng để xác thực bảo mật khi bạn kết nối với GitHub từ máy tính của mình mà không cần nhập mật khẩu mỗi lần truy cập.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Các bước cài đặt SSH key để liên kết tài khoản GitHub với máy tính
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Kiểm tra máy đã có SSH key chưa&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ở màn hình desktop, chuột phải chọn &lt;strong&gt;Git Bash Here&lt;/strong&gt; và chạy lệnh:
&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt; &lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;-al&lt;/span&gt; ~/.ssh
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Nếu có dòng &lt;code&gt;id_rsa&lt;/code&gt;, tức là máy đã có SSH key.&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
  </channel>
</rss>
