JavaScript Variables - Learn Online

Introduction to JavaScript Variables

Lesson Details:
October 21, 2020


I: Introduction

A: Web Development is the task of designing, creating, and updating websites. A web developer’s main concern is with the client-side development of websites, where client refers to the end user’s computer or mobile device. Client-side development includes both front-end development, involving the design of the site’s presentation using HTML, CSS, JavaScript, etc. and Back-end development, which involves the site’s server-side functionality, such as the database.

B: Today, there are many different types of clients that can run a website. Web 2.0 is a term used to refer to websites that allow users to interact and collaborate with each other in a social media dialogue as creators of user-generated content in contrast to websites where users are limited to the passive viewing of content.

II: Body

A: Introduction to javascript variables

JavaScript variables are containers for storing data. They come in 3 types: String, number and object. Strings contain text; numbers contain numerical values; and objects hold collections (arrays) of other objects (numbers, strings, or other objects).

String:

Strings are continuous sequences of characters surrounded by either single or double quotes. Single quotes are used for one-line strings and double quotes for multi-line strings.

var myName = “John”; //single quote var myName = “John Smith”; //double quotes

Number:

Numbers can be represented in several ways. The general form is:

var myNumber = 12345; //decimal number var myNumber = .12345; //decimal number var myNumber = 1234.56; //decimal number var myNumber = 1234e-2; //exponential notation var myNumber = 1e4; //exponential notation var myNumber = 0x1234; //hexadecimal notation var myNumber = 0b101010; //binary notation var myNumber = 1e+1; //scientific notation var myNumber = 1e-1; //turn off scientific notation var myNumber = 1000; //in 1000 it will convert from 1000 to 1k

Object:

Object properties can be accessed much like object members in C#. The property name is specified in the dot operator (.) followed by a pair of parentheses containing the value assigned to the property. The property name must begin with a letter or underscore and may contain alphanumeric characters plus dollar sign ($) or underscore (_). The value may be a string, a number, a Boolean true/false value, a null value (nothing), a function, an array, or another object.

var personName = { firstName: “John”, lastName: “Smith” }; alert(personName); //The following would be invalid because `john` is not a valid identifier var personName = {‘firstName’: ‘John’}; alert(personName); //is equivalent to alert(personName.firstName); alert(personName.lastName); //is equivalent to alert(personName[‘lastName’]); alert(personName[‘firstName’]); //is equivalent to alert(personName[‘firstName’] ); alert(personName[‘lastName’]); //is equivalent to alert(personName[‘lastName’] ); alert(personName[0]); //is equivalent to alert(personName[‘firstName’]); //note that `firstName` is at index 0 var personPhone = { homePhone: { areaCode: 415 }, workPhone: { areaCode: 650 } }; alert(personPhone); alert(personPhone.homePhone); alert(personPhone.workPhone); alert(personPhone.homePhone.areaCode); alert(personPhone.homePhone.areaCode);

loader
Course content