Javascript String Methods and Properties



Finding the window area minus toolbars and scrollbars.

Window Size

<script>
document.getElementById("hw").innerHTML =
"Window width: " + window.innerWidth + "px
" + "Window height: " + window.innerHeight + "px";
</script>

Simple Concatenation

Joining 2 (or more) words through concatenation.

<div id="concat" >
<script>
let text1 = "Pet";
let text2 = "The";
let text3 = "Kitty";
let result = text1.concat(" ", text2, "", text3);
getElementById("concat").innerHTML = result;
</script>

Using Template Literals to allow variables in strings

Concating 2 (or more) words using concatenation with template literals.

I know IE doesn't support these, but they are COOL!

<script>
let firstName = "Daniel";
let lastName = "Hebron";
let text = "Hi ${firstName}, ${lastName}!";
document.getElementById("lit").innerHTML = text;
</script>

Using a an escape sequence to insert a \ into a string

Using an escape character. There are several, I chose to use \.

<script>
let text = "Putting a double backslash into a string will show as \ ";
document.getElementById("demo").innerHTML = text;
</script>

Navigator Object

Using The cookie enabled property will return true if cookies are enabled.

Navigator Object 2

Using the navigator.appName to find the name of the browser.

It will always return Netscape, go figure.

<script>
document.getElementById("apnm").innerHTML =
"navigator.appName is " + navigator.appName;
</script>

The Length Property

Using the Length property to find the length of a string.

<script>
let text = "The Professor always makes these look easy...";
document.getElementById("length").innerHTML = text.length;
</script>

Putting a break in a text string

Using a \ to create a break in a text string

<script>
document.getElementById("brk").innerHTML = "Hello \
Dolly!";
</script>

Using Strings as Objects

Defining a string as an object by Using the word NEW

<script> let x = "Daniel";
let y = new String("Daniel");
document.getElementById("obj").innerHTML =
typeof x + "
" + typeof y;
</script>

Using the indexOf() Method

Using the indexOf() method will show the position of the first instance of a specified text.

<script>
let str = "Show me where the cat is doing cat things to the furniture";
document.getElementById("locate").innerHTML = str.indexOf("cat");
</script>