Welcome to the Javascript Data Types Homepage, Guest!


Javascript | Numbers, Strings, and Objects... oh My!




Data Types

are the tools in which a programmer uses to accomplish goals using real world data. Javascript variables hold the values for numbers, strings, objects, and more which are all data types. Data types are one of the most important topics in programming, I believe. Data types are extremely important because they are essentially adjectives, describing the nature in which the variable will be handled. A number will not be executed in the same way a string will be, and that is why var x = "10"; and var x = 10; will yield unpredictable results. A computer could not possibly solve the following statement without some sense of direction from the programmer; var x = 10 + "fifteen";. Python would probably pick up on the fact that 10 is a number and concatenate the two data types together, but a lot of other languages would end up throwing an error. Javascript will treat the number as a string and concatenate the two together into 10fifteen. Javascript will read each line of code from left to right, and execute the statements as they are read. If JS encounters numbers before a string variable, the output will perform arithmetic on the numbers until reaching a string. If JS encounters a string before a lot of numbers, each individual number following the string will be treated as a string variable. The way we have been mixing data types within Javascript should give a hint that JS types are quite dynamic. Javascript is programmed to allow dynamic data typing, and that means that a variable can hold different data types along the variable's lifecycle.

var pi = 3.14; is a number

var lastName = "Patterson"; is a string with the given value of Patterson.

var person = { firstName:"James", lastName:"Patterson"}; here we declare an object called person with attributes: firstName and lastName.

var x = 10 + "fifteen"; will yield 10fifteen as an output.

var x = 10 + 20 + "fifteen"; Javascript reads lines from left to write, and will add the two numbers together before concatenation.
Answer: 30fifteen

var x = "Pi:" + 3 + 0.1 + 0.04; will yield Pi:30.10.04 because each value is treated as a string value.

var x; x is being declared here, but x still remains undefined.

var x = 23; x is being given the value 23, which is a number.

var x = "Michael Jordan wore number 23"; and now x has been reassigned to a string value.




Strings

Strings are quite popular data types within Javascript, and any programming language for that matter. A string is a text value in which a series of characters are concatenated to create a structure, be it a word, equation, or sentence. Strings are written with quotation marks, and within Javascript you may use single or double quotation marks. If you want to program a quotation mark within your viewable text you must use the other form of quotation inside the quotation.

var moonName = "Luna"; is a string using double quotation marks.

var moonName = 'Luna'; is perfectable acceptable within the syntax of JS.

var fullName = 'James "JP" Patterson'; is acceptable for yielding James "JP" Patterson

var fullName = "James 'JP' Patterson"; will yield the same result but different quotations: James 'JP' Patterson.




Numbers

Javascript only has one type of number data type: Numbers. Numbers can be written as decimals or not. Scientific notation can be written within the Javascript code too.

var pi = 3.14; is a number variable written with decimals.

var mJ = 23; is a number variable written without decimals.

var largeNumber = 987e5; will yield the number 98700000 when executed.

var smallNUmber = 657e-7; will yield the number 0.0000657 when execution occurs.




Booleans

Booleans are important in every programming language. Maybe you want to execute a line of code until some statement returns false, a boolean value will be at the heart of that execution path. If you wish to iterate through a series of numbers until your lucky number is drawn, a boolean will help you achieve this by returning a value of false until your number arrives and returns a true to the statement. Booleans can be extremely useful, but a little difficult to fully understand at first. Booleans are often used in conditional testing within programming. Booleans will only ever have one of two values: true or false.

var t = true; in this declaration t will hold the boolean value true.

var f = false; in this declaration f will be assigned the value false.

Booleans are traditionally used in conditional statements or testing, and we will learn more on booleans later.




Arrays

Javascript arrays are written with [ ] brackets, and the array items are separated by the commas. The following code will create three separate arrays named dogs, cars, and movies. Arrays are zero based data types which means the first item inserted into the array will be indexed at 0, the second is 1, and so forth. Arrays will be discuessed further in a few. Arrays are treated as objects within Javascript.

var dogs = ["Pug", "Chow Chow", "Pit Bull"]

var cars = ["Mustang", "Camaro", "Accord"]

var movies = ["Stephen King's IT", "Dune", "Edward Scissorhands"]




Objects

Javascript uses curly braces in order to write Javascript objects. Objects must have their attribute properties written in the order of valueName: value, and the attributes are separated by commas.

var individual = {firstName:"James", lastName:"Patterson", age:30, eyeColor:"Brown"}

The object 'individual' haas four main properties: firstName, lastName, age, and eyeColor. Objects can be used in many different ways, and we will learn more about objects in a bit.




typeof Operator

The typeof Javascript operator is only used to find the type of Javascript variable a value is. The typeof operator will effectively return the user what type the variable is.

typeof "" will return String

typeof "James" will return String

typeof "James P" returns String

typeof 0 returns a Number

typeof 3.14 Number

typeof (3 + 4) returns a Number again

A primitive data value is a single simple data value with no addition information, properties, or methods associated with it. The typeof operator can return any one of these primitives:

  • String typeof "James"; will return String
  • Number typeof 13; will return Number
  • Boolean typeof true; will return boolean
  • Null var person = null; The value is null within JS, but the person remains an object.
    Remember that null != undefined.
  • Undefined var x; is undefined, and typeof will return undefined
  • Function typeof [1,2,3,4,5]; will return Object, arrays are objects in JS.
    typeof function myFunction(){}; will return the value function.
  • Object typeof {name: "James", age: 30}; will return object.




Next, we will take a look at how Functions are created within Javascript.

Just follow the buttons to continue the tutorial, Guest