Values and types
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
Every value in JavaScript has a type: a number, a string of text, a boolean and five more. Follow one variable through a ten-line script as it holds 42, then "42", then true, and watch typeof report each type, + join text instead of adding, and Number() convert a string back into a number.
5 أسئلة سريعة
اختر إجابة لتعرف السبب. تُحفَظ نتيجتك مع تقدّمك.
01What does typeof "42" give?
The quotes make "42" a string, a piece of text. typeof names the type as a string: "string".
02let x = 42; then x = "hi"; What is true about x?
JavaScript is dynamically typed: a variable can hold a value of any type, and typeof reports the type of the value it holds right now.
03What is "5" + 2?
If either side of + is a string, + joins text: 2 becomes "2" and the result is the string "52".
04How do you get the number 7 from "5" and 2?
Number("5") converts the text into the number 5. Then + has two numbers and adds them: 5 + 2 is 7.
05let y; What is in y?
A variable made without a value holds undefined, which means nothing has been put there yet. null is a value you set yourself to mean empty.
المسرد
- Value
- A single piece of data a program works with, such as 42, "hi" or true.
- Type
- The kind of data a value is (number, string, boolean and so on). It decides what operators like + do with it.
- Primitive
- A simple single value. JavaScript has seven primitive types: number, string, boolean, undefined, null, bigint and symbol.
- String
- A piece of text, written between quotes: "42" is the characters 4 and 2, not the number 42.
- Boolean
- A type with only two values, true and false, used for yes-or-no questions and decisions.
- typeof
- An operator that gives the name of a value's type as a string, such as "number" or "string".
- undefined
- The value a variable holds before anything is put in it; also the name of its type.
- Dynamic typing
- A language feature where variables have no fixed type; only values do, so one variable can hold a number, then a string.
الملاحظات والمصادر
- Every JavaScript number is a 64-bit floating-point value (IEEE 754). Whole numbers are exact up to Number.MAX_SAFE_INTEGER, 9007199254740991 (2^53 − 1); for bigger whole numbers use bigint, written with an n: 9007199254740993n.
- typeof null gives "object", not "null". It is a mistake from the first version of JavaScript, kept so old web pages still work. Check for null with x === null.
- Functions are objects too, but typeof gives "function" for them, so you can tell them apart. Arrays are objects: typeof [1, 2] is "object".
- The console shows a string without its quotes, so "421" appears as 421. Many consoles colour numbers differently from strings so you can still tell them apart.
- The named box is a simplification: x is a binding that refers to its current value. Primitive values themselves never change; giving x a new value swaps in a different value.
- The script is real: paste it into a browser console or save it as types.js and run node types.js. It prints number, string, 421, 43, boolean, undefined.
اختبار يتاح بعد الخطوة الأخيرة.