อยากใช้เครื่องหมาย Quote ใน String ทำยังไงได้บ้าง ?? มาดูกันเลย 🔥
.
👉 เมื่อเราต้องการกำหนด string ใน JavaScript ทำได้โดยกำหนดข้อความ หรือตัวอักษรให้อยู่ภายใต้ single quote (‘ ’) หรือ double quote (“ ”)
.
⚙️ ตัวอย่างเช่น
single quote (‘ ’)
let str1 = 'With Great power come great responsibility';
.
double quote (“ ”)
let str2 = "Your heart is so full of hatred. You are not fit to be king.";
ขอบคุณประโยคเด็ด ๆ จากภาพยนต์เรื่อง Spiderman และ เรื่อง Black Panther
.
👉 หากเราอยากให้มีเครื่อง Quote (‘ ’ หรือ “ ”) ไว้ใน String สามารถทำได้โดย 2 วิธี ดังนี้
.
🌟 1) ใช้เครื่องหมาย Backslash ( \ ) ก่อนหน้าเครื่องหมายคำพูด (“ ”)
📑 ตัวอย่าง 1
let str = '\"BorntoDev Academy\"';
console.log(str);
//"BorntoDev Academy"
.
📑 ตัวอย่าง 2
let myName = "He said, \" My name is Prayut Chan \".";
console.log(myName);
// He said, " My name is Prayut Chan ".
.
🌟 2) ใช้เครื่องหมาย backticks (`) แทน single quote (‘ ’) หรือ double quote (“ ”) ใน String
.
📑 ตัวอย่าง
let string1 = `We're safely using apostrophes and "quotes" in a template literal.`;
console.log(string1);
// We're safely using apostrophes and "quotes" in a template literal.
.
หวังว่าจะเป็นประโยชน์กับเพื่อน ๆ น้าาาา 🥰
.
borntoDev - 🦖 สร้างการเรียนรู้ที่ดีสำหรับสายไอทีในทุกวัน
#javascript #string #quotes #BorntoDev
「javascript template string」的推薦目錄:
- 關於javascript template string 在 BorntoDev Facebook 的最佳解答
- 關於javascript template string 在 BorntoDev Facebook 的最讚貼文
- 關於javascript template string 在 軟體廚房 Facebook 的精選貼文
- 關於javascript template string 在 鐵人賽:JavaScript Template String 樣板字串 - 卡斯伯Blog - 前端 的評價
- 關於javascript template string 在 JavaScript Template Literals 的評價
- 關於javascript template string 在 Convert a string to a template string - Stack Overflow 的評價
- 關於javascript template string 在 medikoo/es6-template-strings: Compile and resolve ... - GitHub 的評價
- 關於javascript template string 在 JavaScript Template Literals - YouTube 的評價
javascript template string 在 BorntoDev Facebook 的最讚貼文
🌟 สวัสดีเพื่อน ๆ วันนี้แอดจะมาแชร์ 3 วิธี ง่าย ๆ ในการต่อ String ในภาษา JavaScript นั่นเอง !! มีวิธีไหน และมีรายละเอียดยังไง ไปดูกันเลยยย
.
1️⃣ ต่อ String ด้วยเครื่องหมายบวก (+)
.
const str1 = "Thailand is";
const str2 = "the land of compromise.";
const newStr = str1+' '+str2;
console.log(newStr);
.
📑 ผลลัพธ์
Thailand is the land of compromise.
.
2️⃣ String Template
เราสามารถสร้าง String Template ด้วยเครื่องหมาย Backticks และใส่ข้อความที่ต้องการให้แสดงลงในประโยคได้ เช่น
.
const Name = "Bang Lee";
const steTemp = `My name is ${Name}.`;
console.log(steTemp);
.
📑 ผลลัพธ์
My name is Bang Lee.
.
3️⃣ ใช้ฟังก์ชัน concat()
.
const string1 = "Me";
const string2 = "You";
const NewString = string1.concat(' & ',string2);
console.log(NewString);
.
📑 ผลลัพธ์
Me & You
*การใช้ concat() ต้องแน่ใจแล้วว่าตัวแปรทั้งสองเป็น String ไม่อย่างงั้นจะ Return TypeError ออกมานั่นเอง
.
ส่วนตัวแอดว่าใช้เครื่องหมายบวก ง่ายสุดละ แล้วเพื่อน ๆ ล่ะ ชอบวิธีไหนกันบ้างงง เมนต์มานะ 😆
.
borntoDev - 🦖 สร้างการเรียนรู้ที่ดีสำหรับสายไอทีในทุกวัน
javascript template string 在 軟體廚房 Facebook 的精選貼文
JavaScript 中也有類似 C# 的 @"" 及 $"" 的字串語法,它叫 Template literals(或 Template strings),從 ES6 開始有。
https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Template_literals
javascript template string 在 JavaScript Template Literals 的推薦與評價
Variable and expression substitutions ... At this point, a template literal is just like a better version of a normal JavaScript string. The big difference ... ... <看更多>
javascript template string 在 Convert a string to a template string - Stack Overflow 的推薦與評價
... <看更多>
javascript template string 在 鐵人賽:JavaScript Template String 樣板字串 - 卡斯伯Blog - 前端 的推薦與評價
鐵人賽:JavaScript Template String 樣板字串. 在過去我們撰寫字串的時候都是使用 " ,這種寫法會讓程式碼變得很攏長且難以閱讀,再多行的時候也必須 ... ... <看更多>