- JavaScript Introduction
- JavaScript Where To
- JavaScript Output
- JavaScript Statements
- JavaScript Syntax
- JavaScript Comments
- JavaScript Variables
- JavaScript Let
- JavaScript Const
- JavaScript Operators
- JavaScript Arithmetic
- JavaScript Assignment
- JavaScript Data Types
- JavaScript Functions
- JavaScript Objects
- JavaScript Events
- JavaScript Strings
- JavaScript String Methods
- JavaScript Numbers
- JavaScript Number Methods
- JavaScript Arrays
- JavaScript Array Const
- JavaScript Array Methods
- JavaScript Sorting Arrays
- JavaScript Array Iteration
- JavaScript Date Objects
- JavaScript Date Formats
- JavaScript Get Date Methods
- JavaScript Set Date Methods
- JavaScript Math Object
- JavaScript Random
- JavaScript Booleans
- JavaScript Comparison And Logical Operators
- JavaScript If Else And Else If
- JavaScript Switch Statement
- JavaScript For Loop
- JavaScript Break And Continue
- JavaScript Type Conversion
- JavaScript Bitwise Operations
- JavaScript Regular Expressions
- JavaScript Errors
- JavaScript Scope
- JavaScript Hoisting
- JavaScript Use Strict
- The JavaScript This Keyword
- JavaScript Arrow Function
- JavaScript Classes
- JavaScript JSON
- JavaScript Debugging
- JavaScript Style Guide
- JavaScript Best Practices
- JavaScript Common Mistakes
- JavaScript Performance
- JavaScript Reserved Words
- JavaScript Versions
- JavaScript History
- JavaScript Forms
- JavaScript Validation API
- JavaScript Objects
- JavaScript Object Properties
- JavaScript Function Definitions
- JavaScript Function Parameters
- JavaScript Function Invocation
- JavaScript Closures
- JavaScript Classes
- Java Script Async
- JavaScript HTML DOM
- The Browser Object Model
- JS Ajax
- JavaScript JSON
- JavaScript Web APIs
- JS Vs JQuery
JavaScript Data Types
JavaScript Data Types
JavaScript variables can store a variety of data types, including numbers, strings, and objects.
var lastName = "Johnson"; // String
var x = {firstName:"John", lastName:"Doe"}; // Object
The Concept Of Data Types
In programming, data types is an important concept.
To be able to operate on variables, it is important to know something about the type.
Without data types, a computer cannot safely solve this:
Does it make any sense to add "Volvo" to sixteen? Will it produce an error or will it produce a result?
JavaScript will treat the example above as:
When adding a number and a string, JavaScript will treat the number as a string.
Example
var x = "Volvo" + 16;JavaScript:
var x = 16 + 4 + "Volvo";Result:
20Volvo
JavaScript:
var x = "Volvo" + 16 + 4;Result:
Volvo164
JavaScript treats 16 and 4 as numbers until it reaches "Volvo" in the first example.
Since the first operand in the second example is a string, all other operands are also treated as strings.
JavaScript Types Are Dynamic
JavaScript types are dynamic. This means that the same variable can be used to store various types of data:
x = 5; // Now x is a Number
x = "John"; // Now x is a String
JavaScript Strings
A string (or a text string) is a series of characters like "John Doe".
Strings are written with quotes. You can use single or double quotes:
var carName2 = 'Volvo XC60'; // Using single quotes
???????
var answer2 = "He is called 'Johnny'"; // Single quotes inside double quotes
var answer3 = 'He is called "Johnny"'; // Double quotes inside single quotes
JavaScript Numbers
JavaScript has only one number type.
It is possible to write numbers with or without decimals:
var x2 = 34; // Written without decimals
Excessively large or extremely small numbers can be written using scientific notation (exponential).
???????
var z = 123e-5; // 0.00123
JavaScript Booleans
Booleans have only two possible values: true or false.
var y = 5;
var z = 6;
(x == y) // Returns true
(x == z) // Returns false
Booleans have only two possible values: true or false.
???????
JavaScript Arrays
Arrays in JavaScript are written using square brackets.
Commas are used to separate array elements.
The code below declares (creates) an array named cars that contains three elements (car names):
JavaScript Objects
Objects in JavaScript are written using curly braces.{}
Separated by commas, object properties are written as name:value pairs.
The Typeof Operator
The JavaScript typeof operator can be used to determine the type of a JavaScript variable.
The typeof operator returns the type of an expression or variable.
typeof "John" // Returns "string"
typeof "John Doe" // Returns "string"
typeof 314 // Returns "number"
typeof 3.14 // Returns "number"
typeof (3) // Returns "number"
typeof (3 + 4) // Returns "number"
Undefined
A variable without a value in JavaScript has the value undefined. Likewise, the type is undefined.
Emptying any variable is possible by setting its value to undefined. In addition, the type will be undefined.
???????
Empty Values
Undefined has nothing to do with an empty value.
The empty string has a valid value and a type.
Null
In JavaScript, null represents nothing. It is intended to be something nonexistent.
Unfortunately, the data type of null in JavaScript is an object.
The fact that typeof null returns an object is a bug in JavaScript. It ought to be null.
Empty an object by setting its value to null:
person = null; // Now value is null, but type is still an object
You may also nullify an object by assigning it the value undefined:
person = undefined; // Now both value and type is undefined
Difference Between Undefined And Null
undefined
and null
are equal in value but different in type:
typeof null // object
null === undefined // false
null == undefined // true
Primitive Data
A primitive data value is a single value with no additional properties or methods.
The typeof operator returns one of the following primitive types:
string
number
boolean
undefined
typeof 3.14 // Returns "number"
typeof true // Returns "boolean"
typeof false // Returns "boolean"
typeof x // Returns "undefined" (if x has no value)
Complex Data
The typeof
operator can return one of two complex types:
function
object
The typeof
operator returns "object" for objects, arrays, and null.
The typeof
operator does not return "object" for functions.
typeof [1,2,3,4] // Returns "object" (not "array", see note below)
typeof null // Returns "object"
typeof function myFunc(){} // Returns "function"
The
typeof
operator returns "object
" for arrays because in JavaScript arrays are objects.
Products
Partner
Copyright © RVR Innovations LLP 2024 | All rights reserved - Mytat.co is the venture of RVR Innovations LLP