Hướng dẫn what is string and number in javascript? - chuỗi và số trong javascript là gì?

TL;DR

Nó phụ thuộc phần lớn vào những gì bạn muốn phân tích cú pháp như một số.

So sánh giữa các chức năng tích hợp

Vì không có nguồn nào hiện tại thỏa mãn tâm hồn tôi, tôi đã cố gắng tìm ra những gì thực sự xảy ra với các chức năng này.

Ba câu trả lời ngay lập tức cho câu hỏi này cảm thấy như:

  1. !isNaN(input) (cung cấp đầu ra giống như +input === +input)
  2. !isNaN(parseFloat(input))
  3. isFinite(input)

Nhưng có ai trong số họ đúng trong mọi kịch bản không?

Tôi đã kiểm tra các chức năng này trong một số trường hợp và tạo ra đầu ra là Markdown. Đây là những gì nó trông giống như:

input!isNaN(input) hoặc +input===+input
+input===+input
!isNaN(
/**
 * Note: JavaScript does not print numeric separator inside a number.
 * In that single case, the markdown output was manually corrected.
 * Also, the comments were manually added later, of course.
 */

let inputs = [
    123, '123', 12.3, '12.3', '   12.3   ',
    1_000_000, '1_000_000',
    '0b11111111', '0o377', '0xFF',
    '', '    ',
    'abc', '12.34Ab!@#$',
    '10e100', '10e1000',
    null, undefined, Infinity];

let markdownOutput = `| \`input\` | \`!isNaN(input)\` or 
\`+input === +input\` | \`!isNaN(parseFloat(input))\` | \`isFinite(input)\` | Comment | | :---: | :---: | :---: | :---: | :--- |\n`; for (let input of inputs) { let outputs = []; outputs.push(!isNaN(input)); outputs.push(!isNaN(parseFloat(input))); outputs.push(isFinite(input)); if (typeof input === 'string') { // Output with quotations console.log(`'${input}'`); markdownOutput += `| '${input}'`; } else { // Output without quotes console.log(input); markdownOutput += `| ${input}`; } for (let output of outputs) { console.log('\t' + output); if (output === true) { markdownOutput += ` |
true
`; // markdownOutput += ` | ✔️`; // for stackoverflow } else { markdownOutput += ` |
false
`; // markdownOutput += ` | ❌`; // for stackoverflow } } markdownOutput += ` ||\n`; } // Replace two or more whitespaces with $nbsp; markdownOutput = markdownOutput.replaceAll(` `, `  `); // Print markdown to console console.log(markdownOutput);
0
/**
 * Note: JavaScript does not print numeric separator inside a number.
 * In that single case, the markdown output was manually corrected.
 * Also, the comments were manually added later, of course.
 */

let inputs = [
    123, '123', 12.3, '12.3', '   12.3   ',
    1_000_000, '1_000_000',
    '0b11111111', '0o377', '0xFF',
    '', '    ',
    'abc', '12.34Ab!@#$',
    '10e100', '10e1000',
    null, undefined, Infinity];

let markdownOutput = `| \`input\` | \`!isNaN(input)\` or 
\`+input === +input\` | \`!isNaN(parseFloat(input))\` | \`isFinite(input)\` | Comment | | :---: | :---: | :---: | :---: | :--- |\n`; for (let input of inputs) { let outputs = []; outputs.push(!isNaN(input)); outputs.push(!isNaN(parseFloat(input))); outputs.push(isFinite(input)); if (typeof input === 'string') { // Output with quotations console.log(`'${input}'`); markdownOutput += `| '${input}'`; } else { // Output without quotes console.log(input); markdownOutput += `| ${input}`; } for (let output of outputs) { console.log('\t' + output); if (output === true) { markdownOutput += ` |
true
`; // markdownOutput += ` | ✔️`; // for stackoverflow } else { markdownOutput += ` |
false
`; // markdownOutput += ` | ❌`; // for stackoverflow } } markdownOutput += ` ||\n`; } // Replace two or more whitespaces with $nbsp; markdownOutput = markdownOutput.replaceAll(` `, `  `); // Print markdown to console console.log(markdownOutput);
1
/**
 * Note: JavaScript does not print numeric separator inside a number.
 * In that single case, the markdown output was manually corrected.
 * Also, the comments were manually added later, of course.
 */

let inputs = [
    123, '123', 12.3, '12.3', '   12.3   ',
    1_000_000, '1_000_000',
    '0b11111111', '0o377', '0xFF',
    '', '    ',
    'abc', '12.34Ab!@#$',
    '10e100', '10e1000',
    null, undefined, Infinity];

let markdownOutput = `| \`input\` | \`!isNaN(input)\` or 
\`+input === +input\` | \`!isNaN(parseFloat(input))\` | \`isFinite(input)\` | Comment | | :---: | :---: | :---: | :---: | :--- |\n`; for (let input of inputs) { let outputs = []; outputs.push(!isNaN(input)); outputs.push(!isNaN(parseFloat(input))); outputs.push(isFinite(input)); if (typeof input === 'string') { // Output with quotations console.log(`'${input}'`); markdownOutput += `| '${input}'`; } else { // Output without quotes console.log(input); markdownOutput += `| ${input}`; } for (let output of outputs) { console.log('\t' + output); if (output === true) { markdownOutput += ` |
true
`; // markdownOutput += ` | ✔️`; // for stackoverflow } else { markdownOutput += ` |
false
`; // markdownOutput += ` | ❌`; // for stackoverflow } } markdownOutput += ` ||\n`; } // Replace two or more whitespaces with $nbsp; markdownOutput = markdownOutput.replaceAll(` `, `  `); // Print markdown to console console.log(markdownOutput);
0
/**
 * Note: JavaScript does not print numeric separator inside a number.
 * In that single case, the markdown output was manually corrected.
 * Also, the comments were manually added later, of course.
 */

let inputs = [
    123, '123', 12.3, '12.3', '   12.3   ',
    1_000_000, '1_000_000',
    '0b11111111', '0o377', '0xFF',
    '', '    ',
    'abc', '12.34Ab!@#$',
    '10e100', '10e1000',
    null, undefined, Infinity];

let markdownOutput = `| \`input\` | \`!isNaN(input)\` or 
\`+input === +input\` | \`!isNaN(parseFloat(input))\` | \`isFinite(input)\` | Comment | | :---: | :---: | :---: | :---: | :--- |\n`; for (let input of inputs) { let outputs = []; outputs.push(!isNaN(input)); outputs.push(!isNaN(parseFloat(input))); outputs.push(isFinite(input)); if (typeof input === 'string') { // Output with quotations console.log(`'${input}'`); markdownOutput += `| '${input}'`; } else { // Output without quotes console.log(input); markdownOutput += `| ${input}`; } for (let output of outputs) { console.log('\t' + output); if (output === true) { markdownOutput += ` |
true
`; // markdownOutput += ` | ✔️`; // for stackoverflow } else { markdownOutput += ` |
false
`; // markdownOutput += ` | ❌`; // for stackoverflow } } markdownOutput += ` ||\n`; } // Replace two or more whitespaces with $nbsp; markdownOutput = markdownOutput.replaceAll(` `, `  `); // Print markdown to console console.log(markdownOutput);
1
/**
 * Note: JavaScript does not print numeric separator inside a number.
 * In that single case, the markdown output was manually corrected.
 * Also, the comments were manually added later, of course.
 */

let inputs = [
    123, '123', 12.3, '12.3', '   12.3   ',
    1_000_000, '1_000_000',
    '0b11111111', '0o377', '0xFF',
    '', '    ',
    'abc', '12.34Ab!@#$',
    '10e100', '10e1000',
    null, undefined, Infinity];

let markdownOutput = `| \`input\` | \`!isNaN(input)\` or 
\`+input === +input\` | \`!isNaN(parseFloat(input))\` | \`isFinite(input)\` | Comment | | :---: | :---: | :---: | :---: | :--- |\n`; for (let input of inputs) { let outputs = []; outputs.push(!isNaN(input)); outputs.push(!isNaN(parseFloat(input))); outputs.push(isFinite(input)); if (typeof input === 'string') { // Output with quotations console.log(`'${input}'`); markdownOutput += `| '${input}'`; } else { // Output without quotes console.log(input); markdownOutput += `| ${input}`; } for (let output of outputs) { console.log('\t' + output); if (output === true) { markdownOutput += ` |
true
`; // markdownOutput += ` | ✔️`; // for stackoverflow } else { markdownOutput += ` |
false
`; // markdownOutput += ` | ❌`; // for stackoverflow } } markdownOutput += ` ||\n`; } // Replace two or more whitespaces with $nbsp; markdownOutput = markdownOutput.replaceAll(` `, `  `); // Print markdown to console console.log(markdownOutput);
2
/**
 * Note: JavaScript does not print numeric separator inside a number.
 * In that single case, the markdown output was manually corrected.
 * Also, the comments were manually added later, of course.
 */

let inputs = [
    123, '123', 12.3, '12.3', '   12.3   ',
    1_000_000, '1_000_000',
    '0b11111111', '0o377', '0xFF',
    '', '    ',
    'abc', '12.34Ab!@#$',
    '10e100', '10e1000',
    null, undefined, Infinity];

let markdownOutput = `| \`input\` | \`!isNaN(input)\` or 
\`+input === +input\` | \`!isNaN(parseFloat(input))\` | \`isFinite(input)\` | Comment | | :---: | :---: | :---: | :---: | :--- |\n`; for (let input of inputs) { let outputs = []; outputs.push(!isNaN(input)); outputs.push(!isNaN(parseFloat(input))); outputs.push(isFinite(input)); if (typeof input === 'string') { // Output with quotations console.log(`'${input}'`); markdownOutput += `| '${input}'`; } else { // Output without quotes console.log(input); markdownOutput += `| ${input}`; } for (let output of outputs) { console.log('\t' + output); if (output === true) { markdownOutput += ` |
true
`; // markdownOutput += ` | ✔️`; // for stackoverflow } else { markdownOutput += ` |
false
`; // markdownOutput += ` | ❌`; // for stackoverflow } } markdownOutput += ` ||\n`; } // Replace two or more whitespaces with $nbsp; markdownOutput = markdownOutput.replaceAll(` `, `  `); // Print markdown to console console.log(markdownOutput);
3
/**
 * Note: JavaScript does not print numeric separator inside a number.
 * In that single case, the markdown output was manually corrected.
 * Also, the comments were manually added later, of course.
 */

let inputs = [
    123, '123', 12.3, '12.3', '   12.3   ',
    1_000_000, '1_000_000',
    '0b11111111', '0o377', '0xFF',
    '', '    ',
    'abc', '12.34Ab!@#$',
    '10e100', '10e1000',
    null, undefined, Infinity];

let markdownOutput = `| \`input\` | \`!isNaN(input)\` or 
\`+input === +input\` | \`!isNaN(parseFloat(input))\` | \`isFinite(input)\` | Comment | | :---: | :---: | :---: | :---: | :--- |\n`; for (let input of inputs) { let outputs = []; outputs.push(!isNaN(input)); outputs.push(!isNaN(parseFloat(input))); outputs.push(isFinite(input)); if (typeof input === 'string') { // Output with quotations console.log(`'${input}'`); markdownOutput += `| '${input}'`; } else { // Output without quotes console.log(input); markdownOutput += `| ${input}`; } for (let output of outputs) { console.log('\t' + output); if (output === true) { markdownOutput += ` |
true
`; // markdownOutput += ` | ✔️`; // for stackoverflow } else { markdownOutput += ` |
false
`; // markdownOutput += ` | ❌`; // for stackoverflow } } markdownOutput += ` ||\n`; } // Replace two or more whitespaces with $nbsp; markdownOutput = markdownOutput.replaceAll(` `, `  `); // Print markdown to console console.log(markdownOutput);
3
Bình luận
123 -
'123'-
12.3 -
'123'-
'123'-
'123'-
'123''12 .3 ''12 .3 ''& nbsp; & nbsp; 12.3 & nbsp; & nbsp; '
Khoảng trắng rỗng được cắt tỉa, như mong đợi.-
'123'-
'123'-
'123''12 .3 '-
'123''12 .3 '-
'123''12 .3 ''12 .3 ''12 .3 ''& nbsp; & nbsp; 12.3 & nbsp; & nbsp; '
Khoảng trắng rỗng được cắt tỉa, như mong đợi.'12 .3 ''12 .3 ''& nbsp; & nbsp; 12.3 & nbsp; & nbsp; '
Khoảng trắng rỗng được cắt tỉa, như mong đợi.-
But caution! It's way more larger than the maximum safe integer value 253 (about 9×1015). Read this for details.
'123''12 .3 ''& nbsp; & nbsp; 12.3 & nbsp; & nbsp; '
Though not as crazy as it may seem. In JavaScript, a value larger than ~10308 is rounded to infinity, that's why. Look here for details.
And yes,
/**
 * Note: JavaScript does not print numeric separator inside a number.
 * In that single case, the markdown output was manually corrected.
 * Also, the comments were manually added later, of course.
 */

let inputs = [
    123, '123', 12.3, '12.3', '   12.3   ',
    1_000_000, '1_000_000',
    '0b11111111', '0o377', '0xFF',
    '', '    ',
    'abc', '12.34Ab!@#$',
    '10e100', '10e1000',
    null, undefined, Infinity];

let markdownOutput = `| \`input\` | \`!isNaN(input)\` or 
\`+input === +input\` | \`!isNaN(parseFloat(input))\` | \`isFinite(input)\` | Comment | | :---: | :---: | :---: | :---: | :--- |\n`; for (let input of inputs) { let outputs = []; outputs.push(!isNaN(input)); outputs.push(!isNaN(parseFloat(input))); outputs.push(isFinite(input)); if (typeof input === 'string') { // Output with quotations console.log(`'${input}'`); markdownOutput += `| '${input}'`; } else { // Output without quotes console.log(input); markdownOutput += `| ${input}`; } for (let output of outputs) { console.log('\t' + output); if (output === true) { markdownOutput += ` |
true
`; // markdownOutput += ` | ✔️`; // for stackoverflow } else { markdownOutput += ` |
false
`; // markdownOutput += ` | ❌`; // for stackoverflow } } markdownOutput += ` ||\n`; } // Replace two or more whitespaces with $nbsp; markdownOutput = markdownOutput.replaceAll(` `, `  `); // Print markdown to console console.log(markdownOutput);
5 considers infinity as a number, and
/**
 * Note: JavaScript does not print numeric separator inside a number.
 * In that single case, the markdown output was manually corrected.
 * Also, the comments were manually added later, of course.
 */

let inputs = [
    123, '123', 12.3, '12.3', '   12.3   ',
    1_000_000, '1_000_000',
    '0b11111111', '0o377', '0xFF',
    '', '    ',
    'abc', '12.34Ab!@#$',
    '10e100', '10e1000',
    null, undefined, Infinity];

let markdownOutput = `| \`input\` | \`!isNaN(input)\` or 
\`+input === +input\` | \`!isNaN(parseFloat(input))\` | \`isFinite(input)\` | Comment | | :---: | :---: | :---: | :---: | :--- |\n`; for (let input of inputs) { let outputs = []; outputs.push(!isNaN(input)); outputs.push(!isNaN(parseFloat(input))); outputs.push(isFinite(input)); if (typeof input === 'string') { // Output with quotations console.log(`'${input}'`); markdownOutput += `| '${input}'`; } else { // Output without quotes console.log(input); markdownOutput += `| ${input}`; } for (let output of outputs) { console.log('\t' + output); if (output === true) { markdownOutput += ` |
true
`; // markdownOutput += ` | ✔️`; // for stackoverflow } else { markdownOutput += ` |
false
`; // markdownOutput += ` | ❌`; // for stackoverflow } } markdownOutput += ` ||\n`; } // Replace two or more whitespaces with $nbsp; markdownOutput = markdownOutput.replaceAll(` `, `  `); // Print markdown to console console.log(markdownOutput);
4 parses infinity as infinity.
Khoảng trắng rỗng được cắt tỉa, như mong đợi.'12 .3 '-
Then why
/**
 * Note: JavaScript does not print numeric separator inside a number.
 * In that single case, the markdown output was manually corrected.
 * Also, the comments were manually added later, of course.
 */

let inputs = [
    123, '123', 12.3, '12.3', '   12.3   ',
    1_000_000, '1_000_000',
    '0b11111111', '0o377', '0xFF',
    '', '    ',
    'abc', '12.34Ab!@#$',
    '10e100', '10e1000',
    null, undefined, Infinity];

let markdownOutput = `| \`input\` | \`!isNaN(input)\` or 
\`+input === +input\` | \`!isNaN(parseFloat(input))\` | \`isFinite(input)\` | Comment | | :---: | :---: | :---: | :---: | :--- |\n`; for (let input of inputs) { let outputs = []; outputs.push(!isNaN(input)); outputs.push(!isNaN(parseFloat(input))); outputs.push(isFinite(input)); if (typeof input === 'string') { // Output with quotations console.log(`'${input}'`); markdownOutput += `| '${input}'`; } else { // Output without quotes console.log(input); markdownOutput += `| ${input}`; } for (let output of outputs) { console.log('\t' + output); if (output === true) { markdownOutput += ` |
true
`; // markdownOutput += ` | ✔️`; // for stackoverflow } else { markdownOutput += ` |
false
`; // markdownOutput += ` | ❌`; // for stackoverflow } } markdownOutput += ` ||\n`; } // Replace two or more whitespaces with $nbsp; markdownOutput = markdownOutput.replaceAll(` `, `  `); // Print markdown to console console.log(markdownOutput);
7 should return a
/**
 * Note: JavaScript does not print numeric separator inside a number.
 * In that single case, the markdown output was manually corrected.
 * Also, the comments were manually added later, of course.
 */

let inputs = [
    123, '123', 12.3, '12.3', '   12.3   ',
    1_000_000, '1_000_000',
    '0b11111111', '0o377', '0xFF',
    '', '    ',
    'abc', '12.34Ab!@#$',
    '10e100', '10e1000',
    null, undefined, Infinity];

let markdownOutput = `| \`input\` | \`!isNaN(input)\` or 
\`+input === +input\` | \`!isNaN(parseFloat(input))\` | \`isFinite(input)\` | Comment | | :---: | :---: | :---: | :---: | :--- |\n`; for (let input of inputs) { let outputs = []; outputs.push(!isNaN(input)); outputs.push(!isNaN(parseFloat(input))); outputs.push(isFinite(input)); if (typeof input === 'string') { // Output with quotations console.log(`'${input}'`); markdownOutput += `| '${input}'`; } else { // Output without quotes console.log(input); markdownOutput += `| ${input}`; } for (let output of outputs) { console.log('\t' + output); if (output === true) { markdownOutput += ` |
true
`; // markdownOutput += ` | ✔️`; // for stackoverflow } else { markdownOutput += ` |
false
`; // markdownOutput += ` | ❌`; // for stackoverflow } } markdownOutput += ` ||\n`; } // Replace two or more whitespaces with $nbsp; markdownOutput = markdownOutput.replaceAll(` `, `  `); // Print markdown to console console.log(markdownOutput);
8 here? Someone please explain this design concept to me.
'123''12 .3 ''12 .3 ''12 .3 ''& nbsp; & nbsp; 12.3 & nbsp; & nbsp; '
Khoảng trắng rỗng được cắt tỉa, như mong đợi.'12 .3 ''& nbsp; & nbsp; 12.3 & nbsp; & nbsp; '

Khoảng trắng rỗng được cắt tỉa, như mong đợi.

1_000_000it depends largely on what we need. For example, we may want to consider a null input as 0. In that case !isNaN(input)1 will work fine.

Phân tách số hiểu, cũng được mong đợi.

'1_000_000'

/**
 * My necessity was met by the following code.
 */

if (input === null) {
    // Null input
} else if (input.trim() === '') {
    // Empty or whitespace-only string
} else if (isFinite(input)) {
    // Input is a number
} else {
    // Not a number
}

Bất ngờ! JS sẽ không phân tách phân tách số bên trong một chuỗi. Để biết chi tiết, kiểm tra vấn đề này. (Tại sao sau đó phân tích cú pháp khi phao hoạt động mặc dù? Chà, nó không. 😉)

/**
 * Note: JavaScript does not print numeric separator inside a number.
 * In that single case, the markdown output was manually corrected.
 * Also, the comments were manually added later, of course.
 */

let inputs = [
    123, '123', 12.3, '12.3', '   12.3   ',
    1_000_000, '1_000_000',
    '0b11111111', '0o377', '0xFF',
    '', '    ',
    'abc', '12.34Ab!@#$',
    '10e100', '10e1000',
    null, undefined, Infinity];

let markdownOutput = `| \`input\` | \`!isNaN(input)\` or 
\`+input === +input\` | \`!isNaN(parseFloat(input))\` | \`isFinite(input)\` | Comment | | :---: | :---: | :---: | :---: | :--- |\n`; for (let input of inputs) { let outputs = []; outputs.push(!isNaN(input)); outputs.push(!isNaN(parseFloat(input))); outputs.push(isFinite(input)); if (typeof input === 'string') { // Output with quotations console.log(`'${input}'`); markdownOutput += `| '${input}'`; } else { // Output without quotes console.log(input); markdownOutput += `| ${input}`; } for (let output of outputs) { console.log('\t' + output); if (output === true) { markdownOutput += ` |
true
`; // markdownOutput += ` | ✔️`; // for stackoverflow } else { markdownOutput += ` |
false
`; // markdownOutput += ` | ❌`; // for stackoverflow } } markdownOutput += ` ||\n`; } // Replace two or more whitespaces with $nbsp; markdownOutput = markdownOutput.replaceAll(` `, `  `); // Print markdown to console console.log(markdownOutput);

Sự khác biệt giữa chuỗi và số trong JavaScript là gì?

Khi so sánh một chuỗi với một số, JavaScript sẽ chuyển đổi chuỗi thành một số khi thực hiện so sánh.Một chuỗi trống chuyển đổi thành 0. Một chuỗi không phải là số chuyển đổi thành NAN luôn là sai.Khi so sánh hai chuỗi, "2" sẽ lớn hơn "12", vì (theo thứ tự bảng chữ cái) 1 là nhỏ hơn 2.JavaScript will convert the string to a number when doing the comparison. An empty string converts to 0. A non-numeric string converts to NaN which is always false . When comparing two strings, "2" will be greater than "12", because (alphabetically) 1 is less than 2.

Số chuỗi trong JavaScript là gì?

Một chuỗi số (như "3.14") chuyển đổi thành một số (như 3.14).Một chuỗi trống (như "" ") chuyển đổi thành 0. Một chuỗi không số (như" John ") chuyển đổi thành nan (không phải là một số).. An empty string (like "") converts to 0. A non numeric string (like "John") converts to NaN (Not a Number).

Chuỗi và số là gì?

Integer là một giá trị số, trong khi chuỗi là một giá trị ký tự được biểu thị trong các trích dẫn..

Một chuỗi trong JavaScript là gì?

Một chuỗi là một chuỗi của một hoặc nhiều ký tự có thể bao gồm các chữ cái, số hoặc ký hiệu.Các chuỗi trong JavaScript là các loại dữ liệu nguyên thủy và bất biến, điều đó có nghĩa là chúng không thay đổi.a sequence of one or more characters that may consist of letters, numbers, or symbols. Strings in JavaScript are primitive data types and immutable, which means they are unchanging.