0% found this document useful (0 votes)
34K views

Data Types

There are 8 basic data types in JavaScript. The typeof operator allows us to see which type is stored in a variable. Two forms: typeof x or typeof(x). Returns a string with the name of the type, like "string".

Uploaded by

suchita
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34K views

Data Types

There are 8 basic data types in JavaScript. The typeof operator allows us to see which type is stored in a variable. Two forms: typeof x or typeof(x). Returns a string with the name of the type, like "string".

Uploaded by

suchita
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Data types

1. A value in JavaScript is always of a certain type. For


example, a string or a number.
2. There are eight basic data types in JavaScript.

Eg:-
let message = "hello";
message = 123456;
Note:- JavaScript, are called “dynamically typed”,
meaning that there exist data types, but variables are not
bound to any of them.
NUMBER
• The number type represents both integer and floating point numbers.
• There are many operations for numbers, e.g. multiplication *, division /,
addition +, subtraction -, etc.,
• Besides regular numbers, there are so-called “special numeric values”
which also belong to this data type: Infinity, -Infinity and NaN.
• Infinity represents the mathematical Infinity ∞. It is a special value
that’s greater than any number.
• NaN represents a computational error.
• It is a result of an incorrect or an undefined mathematical operation
STRINGS
• A string in JavaScript must be surrounded by quotes.
Syntax:
let str = "Hello";
let str2 = 'Single quotes are ok too';
let phrase = `can embed another ${str}`;

In JavaScript, there are 3 types of quotes.


• Double quotes: "Hello".
• Single quotes: 'Hello'.
• Backticks: `Hello`.
• Backticks are “extended functionality” quotes.
• They allow us to embed variables and
expressions into a string by wrapping them
in ${…},
Eg:-
let name = “JAVA";
// embed a variable
alert( `Hello, ${name}!` ); // Hello, JAVA!
// embed an expression
alert( `the result is ${1 + 2}` ); // the result is 3
There is no character type.
• In some languages, there is a special
“character” type for a single character. For
example, in the C language and in Java it is
called “char”.
• In JavaScript, there is no such type. There’s
only one type: string. A string may consist of
zero characters (be empty), one character or
many of them.
BOOLEAN (LOGICAL TYPE)
• The boolean type has only two values: true and false.
• This type is commonly used to store yes/no
values: true means “yes, correct”, and false means “no,
incorrect”.
let nameFieldChecked = true; // yes, name field is checked
let ageFieldChecked = false; // no, age field is not checked
• Boolean values also come as a result of comparisons:
let isGreater = 14 > 10;
alert( isGreater ); // true (the comparison result is "yes")
The “null” value
• The special null value does not belong to any of the types
discussed above.
• It forms a separate type of its own which contains only
the null value:
• In JavaScript, null is not a “reference to a non-existing
object” or a “null pointer” like in some other languages.
• It’s just a special value which represents “nothing”,
“empty” or “value unknown”.
• The code above states that name is unknown.
let name = null;
The “undefined” value
• The special value undefined also stands apart. It makes a type of its own, just
like null.
• The meaning of undefined is “value is not assigned”.
• If a variable is declared, but not assigned, then its value is undefined:
let age;
alert(age); // shows "undefined“
----------------
let age = 100; // change the value to undefined
age = undefined;
alert(age); // "undefined“
----------------
Normally, one uses null to assign an “empty” or “unknown” value to a variable,
while undefined is reserved as a default initial value for unassigned things.
Objects & Symbols
• The object type is special.
• All other types are called “primitive” because
their values can contain only a single thing (be
it a string or a number or whatever).
• In contrast, objects are used to store
collections of data and more complex entities.
• The symbol type is used to create unique
identifiers for objects.
The typeof operator
• The typeof operator returns the type of the argument.
• It’s useful when we want to process values of different
types differently or just want to do a quick check.
• It supports two forms of syntax:
As an operator: typeof x.
As a function: typeof(x).
In other words, it works with parentheses or without
them.
typeof undefined // "undefined"
typeof 0 // "number"
typeof 10n // "bigint"
typeof true // "boolean"
typeof "foo" // "string"
typeof Symbol("id") // "symbol"
typeof Math // "object" (1)
typeof null // "object" (2)
typeof alert // "function" (3)
SUMMARY
There are 8 basic data types in JavaScript.
• number for numbers of any kind: integer or floating-point, integers are limited
by ±(253-1).
• bigint is for integer numbers of arbitrary length.
• string for strings. A string may have zero or more characters, there’s no separate
single-character type.
• boolean for true/false.
• null for unknown values – a standalone type that has a single value null.
• undefined for unassigned values – a standalone type that has a single value undefined.
• object for more complex data structures.
• symbol for unique identifiers.
• The typeof operator allows us to see which type is stored in a variable.
– Two forms: typeof x or typeof(x).
• Returns a string with the name of the type, like "string".
Task
let name = “Seema";
alert( `hello ${123}` ); // output?
alert( `hello ${"name"}` ); // output?
alert( `hello ${name}` ); // output?

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy